Factory Metod
Factory Metod
<?php abstract class Button { protected $_html; public function getHtml(){ return $this->_html; } } class ImageButton extends Button { //This should be whatever HTML you want for your image-based button protected $_html = ' Image Button '; } class InputButton extends Button { protected $_html = ' Input Button '; } class FlashButton extends Button {protected $_html = ' Flash Button '; }
class ButtonFactory { public static function createButton($type) { $baseClass = 'Button'; $targetClass = ucfirst($type).$baseClass; if (class_exists($targetClass) && is_subclass_of($targetClass, $baseClass)) { return new $targetClass; } else { throw new Exception("The button type '$type' is not recognized."); } } }
$buttons = array('image','input','flash'); foreach($buttons as $b) { echo "<br>".ButtonFactory::createButton($b)->getHtml()."<br>"; } echo "<br>". ButtonFactory::createButton("new")->getHtml(); ?>
interface IUser { //cant use getter setter as no property in interface //public function getUserName(); //public function setUserName($name); //Other getter setter methods }
abstract public function showUserInfo(); /* public abstract function addTeacher(); public abstract function addStudent(); public abstract function searchTeacher(); public abstract function searchStudent(); public abstract function editStudent(); public abstract function editTeacher(); public abstract function showUserInfo(); */ }
final class Admin extends UserAbstract implements IUser { private $name="anirudh"; private static $count = 0; //private $p= new Priviliges();
public $p; public function __construct($name){ echo "<br> constructor called"; $this->name=$name; self::$count++; $this->p = new Priviliges(); echo $p->a; }
public function showPriviliges() { echo "Priviliges ".$this->p->a; } public static function getCount() { return self::$count; }
//public function __clone() { echo "I've been cloned"; } function getUserName(){ return $this->name; }
function setUserName($name){ $this->name=$name; } //Other getter setter methods public function showUserInfo(){ echo "<br> Name: $this->name"; }
if($method="same" && !isset($arg[0])) { echo "<br>same() 1 ..."; } if($method="same" && isset($arg[0])) { echo "<br>same(\"two\")...."; }
echo "<br> creating object"; $obj=new Admin("sheenabh"); $obj->setUserName("newname"); echo "<br> after setting value <br>"; echo $obj->getUserName(); echo $obj->showUserInfo();
echo $obj->same(); echo $obj->same("two"); echo "-------------------"; echo "<br> Serializing <br>"; $s = serialize($obj); echo "<br>Storing admin obj in file<br>"; file_put_contents('store.ser', $s);//Store is a file name echo "<br>Reading admin obj from file<br>"; $s1 = file_get_contents('store.ser'); echo "<br>Unserializing<br>"; $a = unserialize($s1); echo "<br>Displaying admin info using obj<br>"; $a->showUserInfo();
$c=clone $obj; $obj->setUserName("Another name"); echo "<br>Displaying admin info using obj<br>"; $obj->showUserInfo(); echo "<br>Displaying admin info using cloned<br>"; $c->showUserInfo(); echo"<br<br>----------------Display privilige value when object created in admin class"; $obj->showPriviliges(); } ?> Oops
final class Admin { private $name="anirudh"; private static $count = 0; //private $p= new Priviliges();
public $p; public function __construct($name){ echo "<br> constructor called"; $this->name=$name; self::$count++; $this->p = new Priviliges(); echo $p->a; }
//public function __clone() { echo "I've been cloned"; } function getUserName(){ return $this->name; }
function setUserName($name){ $this->name=$name; } //Other getter setter methods public function showUserInfo(){ echo "<br> Name: $this->name"; }
echo "<br> creating object"; $obj=new Admin("ani"); $obj->setUserName("newname"); echo "<br> after setting value <br>"; echo $obj->getUserName(); echo $obj->showUserInfo(); echo $obj->same(); echo $obj->same("two");
echo "-------------------"; echo "<br> Serializing <br>"; $s = serialize($obj); echo "<br>Storing admin obj in file<br>"; file_put_contents('store.ser', $s);//Store is a file name echo "<br>Reading admin obj from file<br>"; $s1 = file_get_contents('store.ser'); echo "<br>Unserializing<br>"; $a = unserialize($s1); echo "<br>Displaying admin info using obj<br>"; $a->showUserInfo();
$c=clone $obj; $obj->setUserName("Another name"); echo "<br>Displaying admin info using obj<br>"; $obj->showUserInfo(); echo "<br>Displaying admin info using cloned<br>"; $c->showUserInfo(); echo"<br<br>----------------Display privilige value when object created in admin class"; $obj->showPriviliges(); } ?>
Namespace <?php
require 'oops.php'; use Anirudh\Admin as Admin; echo "<br> <br>-------------------------------"; $w=new Admin("new name"); $w->showUserInfo(); echo "<br> <br> No of admins created: ".Admin::getCount(); ?> Test <?php error_reporting ( E_ALL ); class Person { public $name = 'Anirudh'; // this is initialization public $age; public function __construct($name, $age) { if (! empty ( $name )) { $this->name = $name; } $this->age = $age; } public function introduce() { echo "I'm {$this->name} and I'm {$this->age} years old<br/>"; } public function __destruct() { echo "Bye for now<br/>";
if( is_null(self::$instance) ) {
} return self::$instance;
} private function __construct() { } private function __clone() { } // any other DB methods we might use } class self1{
// get a session instance $db = DB::getInstance(); echo "----".$db; ?> Prop <?php class candy{ public $type='chocolate'; public $choctype = array(); public function wrap(){ echo 'Its a wrap'; } public function __get($index){ echo 'Retrieving element of $choctype property with index of '.$index.'<br />'; return $this->choctype[$index]; }
$candy->milk=10; $candy->toffee=20; echo "<br><br>The value of the added properties are <br><br> $candy->milk <br><br>$candy->toffee";
class Mammal extends Animal { protected $abc=11; public function getABC() { echo $this->abc."<br/>"; }
class Mammal1 extends Animal { protected $abc=12; public function getABC() { echo $this->abc."<br/>"; }
$mammal=new Mammal1();//animal method called // $animal = new Animal();// mammal method called
?>