0% found this document useful (0 votes)
52 views16 pages

Factory Metod

The document discusses PHP code examples demonstrating factory method pattern, abstract classes, interfaces, serialization, cloning objects, singletons, properties, and polymorphism. The code shows creating button objects using a factory method, defining an abstract user class and interface, serializing and cloning an admin object, creating a singleton database class, using magic methods for properties, and calling polymorphic methods on different animal classes.

Uploaded by

kiranmann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views16 pages

Factory Metod

The document discusses PHP code examples demonstrating factory method pattern, abstract classes, interfaces, serialization, cloning objects, singletons, properties, and polymorphism. The code shows creating button objects using a factory method, defining an abstract user class and interface, serializing and cloning an admin object, creating a singleton database class, using magic methods for properties, and calling polymorphic methods on different animal classes.

Uploaded by

kiranmann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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(); ?>

Oops <?php namespace Anirudh { error_reporting ( E_ALL );

interface IUser { //cant use getter setter as no property in interface //public function getUserName(); //public function setUserName($name); //Other getter setter methods }

abstract class UserAbstract { //all common properties here

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"; }

public function __call($method, $arg){

if($method="same" && !isset($arg[0])) { echo "<br>same() 1 ..."; } if($method="same" && isset($arg[0])) { echo "<br>same(\"two\")...."; }

class Priviliges { public $a="--privilige--"; }

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

<?php namespace Anirudh { error_reporting ( E_ALL );

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 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"; }

public function __call($method, $arg){

if($method="same" && !isset($arg[0])) {

echo "<br>same() 1 ..."; } if($method="same" && isset($arg[0])) { echo "<br>same(\"two\")...."; }

class Priviliges { public $a="--privilige--"; }

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/>";

} } $person = new Person ( "new" ); $person->age = 20; $person->introduce ();

?> Singleton <?php class DB{

private static $instance;

public static function getInstance() {

if( is_null(self::$instance) ) {

self::$instance = new self1();

echo "instance created";

} 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]; }

public function __set($index, $value){ $this->choctype[$index]=$value; } } $candy = new candy();

$candy->milk=10; $candy->toffee=20; echo "<br><br>The value of the added properties are <br><br> $candy->milk <br><br>$candy->toffee";

?> Poly <?php //echo "using polymorphism"."<br/>";

class Animal { protected $abc=10; public function getABC() { echo $this->abc."<br/>"; }

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/>"; }

class ABC { public function getPoly(Animal $an){ echo $an->getABC(); } }

$mammal=new Mammal1();//animal method called // $animal = new Animal();// mammal method called

// $animal->getABC(); // $mammal->getABC(); $abc = new ABC(); $abc->getPoly($mammal);

?>

You might also like