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!

 
 

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!

 
 

The Method Of A Child Class Overriding That Of Its Parent

  <html> <head> <title>The Method of a Child Class Overriding That of Its Parent</title> </head> <body> <?php class FirstClass{ var $name = "first class"; function FirstClass( $n ){ $this->name = $n; } function sayHello(){ print "Hello my name is $this->name<br />"; } }class second_class extends FirstClass { function sayHello(){ print "say hi<br />"; } }$test […]
Continue reading…

Enjoyed this post? Share it!

 
 

Use This Keyword To Reference Interal Properties

  <?php class Employee { public $numPages; public $stayYear; public $title; public $lastName; public $firstName; public $price;   function __construct( $title, $firstName, $mainName, $price, $numPages=0, $stayYear=0 ) { $this->title = $title; $this->firstName = $firstName; $this->lastName = $mainName; $this->price = $price; $this->numPages = $numPages; $this->stayYear = $stayYear; }   function getNumberOfPages() { return $this->numPages; } function […]
Continue reading…

Enjoyed this post? Share it!