Using Default Constructors

<?php class Dog { function __construct($name=’No-name’, $breed=’breed unknown’, $price = 15) { $this->name = $name; $this->breed = $breed; $this->price = $price; } } $aDog = new Dog(); $tweety = new Dog(’A’, ‘a’); printf("<p>%s is a %s and costs \$%.2f.</p>\n", $aDog->name, $aDog->breed, $aDog->price); $tweety->price = 24.95; printf("<p>%s is a %s and costs \$%.2f.</p>\n", $tweety->name, $tweety->breed, $tweety->price); […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using Inheritance To Efficiently Represent Various Vehicle Types

<? class Vehicle { var $model; var $current_speed; function setSpeed($mph) { $this->current_speed = $mph; } function getSpeed() { return $this->current_speed; } }class Auto extends Vehicle { var $fuel_type; function setFuelType($fuel) { $this->fuel_type = $fuel; } function getFuelType() { return $this->fuel_type; }}class Airplane extends Vehicle { var $wingspan; function setWingSpan($wingspan) { $this->wingspan = $wingspan; } function […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using __Sleep() And __Wakeup() For Objects

<?php class UserClass { public $sessionID; public $username; public function __sleep() { session_destroy(); return array("username"); } public function __wakeup() { session_start(); $this->sessionId = session_id(); } } session_start(); $user = new UserClass; $user->sessionId = session_id(); $seralized_user = serialize($user); unset($user); $user = unserialize($serialized_user); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Using Type Hinting With Interfaces

<?php interface printable { public function printme(); } abstract class Number { private $value; abstract public function value(); public function reset() { $this->value = NULL; } } class Integer extends Number implements printable { private $value; function __construct($value) { $this->value = $value; } public function getValue() { return (int)$this->value; } public function printme() { echo […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using Static Methods And Properties To Limit Instances Of A Class (Php 5 Only)

<?phpclass Shop { private static $instance; public $name="shop"; private function ___construct() { } public static function getInstance() { if ( empty( self::$instance ) ) { self::$instance = new Shop(); } return self::$instance; } }$first = Shop::getInstance(); $first-> name="A";$second = Shop::getInstance(); print $second -> name; ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Var_Dump A Class

  <?php class Person { private $name; private $age; private $id; function __construct( $name, $age ) { $this->name = $name; $this->age = $age; } function setId( $id ) { $this->id = $id; }   function getId(){ echo "get id method"; }   function __clone() { $this->id = 0; } }var_dump( new Person("A",10) );?>
Continue reading…

Enjoyed this post? Share it!