1Example 1: A singleton (static members) 2======================================= 3 4<?php 5 6 class Counter { 7 var $counter = 0; 8 9 function increment_and_print() 10 { 11 print ++$this->counter; 12 print "\n"; 13 } 14 } 15 16 17 class SingletonCounter { 18 static $m_instance = NULL; 19 20 function Instance() 21 { 22 if (self::$m_instance == NULL) { 23 self::$m_instance = new Counter(); 24 } 25 return self::$m_instance; 26 } 27 } 28 29 SingletonCounter::Instance()->increment_and_print(); 30 SingletonCounter::Instance()->increment_and_print(); 31 SingletonCounter::Instance()->increment_and_print(); 32 33?> 34 35Example 2: Factory method (derefencing objects returned from functions) 36======================================================================= 37 38<?php 39 40 class Circle { 41 function draw() 42 { 43 print "Circle\n"; 44 } 45 } 46 47 class Square { 48 function draw() 49 { 50 print "Square\n"; 51 } 52 } 53 54 function ShapeFactoryMethod($shape) 55 { 56 switch ($shape) { 57 case "Circle": 58 return new Circle(); 59 case "Square": 60 return new Square(); 61 } 62 } 63 64 ShapeFactoryMethod("Circle")->draw(); 65 ShapeFactoryMethod("Square")->draw(); 66 67 68?> 69 70Example 3: Class constants and class scope 71========================================== 72 73<?php 74 75 class ErrorCodes { 76 const FATAL = "Fatal error\n"; 77 const WARNING = "Warning\n"; 78 const INFO = "Informational message\n"; 79 80 function print_fatal_error_codes() 81 { 82 print "FATAL = " . FATAL; 83 print "self::FATAL = " . self::FATAL; 84 } 85 } 86 87 /* Call the static function and move into the ErrorCodes scope */ 88 ErrorCodes::print_fatal_error_codes(); 89 90?> 91 92Example 4: Regular object method using both local and global functions 93====================================================================== 94 95<?php 96 97 class HelloWorld { 98 const HELLO_WORLD = "Hello, World!\n"; 99 100 function get_hello_world() 101 { 102 return HELLO_WORLD; 103 } 104 105 function length_of_hello_world() 106 { 107 $str = $this->get_hello_world(); 108 return strlen($str); 109 } 110 } 111 112 $obj = new HelloWorld(); 113 print "length_of_hello_world() = " . $obj->length_of_hello_world(); 114 print "\n"; 115?> 116 117Example 5: Multiple derefencing of objects returned from methods 118================================================================ 119 120<?php 121 122 123 class Name { 124 function Name($_name) 125 { 126 $this->name = $_name; 127 } 128 129 function display() 130 { 131 print $this->name; 132 print "\n"; 133 } 134 } 135 136 class Person { 137 function Person($_name, $_address) 138 { 139 $this->name = new Name($_name); 140 } 141 142 function getName() 143 { 144 return $this->name; 145 } 146 } 147 148 $person = new Person("John", "New York"); 149 $person->getName()->display(); 150 151?> 152 153Example 6: Exception handling 154============================= 155 156<? 157 class MyException { 158 function MyException($_error) { 159 $this->error = $_error; 160 } 161 162 function getException() 163 { 164 return $this->error; 165 } 166 } 167 168 function ThrowException() 169 { 170 throw new MyException("'This is an exception!'"); 171 } 172 173 174 try { 175 } catch (MyException $exception) { 176 print "There was an exception: " . $exception->getException(); 177 print "\n"; 178 } 179 180 try { 181 ThrowException(); 182 } catch (MyException $exception) { 183 print "There was an exception: " . $exception->getException(); 184 print "\n"; 185 } 186 187?> 188 189Example 7: __clone() 190=================== 191 192<? 193 class MyCloneable { 194 static $id = 0; 195 196 function MyCloneable() 197 { 198 $this->id = self::$id++; 199 } 200 201 function __clone() 202 { 203 $this->name = $that->name; 204 $this->address = "New York"; 205 $this->id = self::$id++; 206 } 207 } 208 209 210 211 $obj = new MyCloneable(); 212 213 $obj->name = "Hello"; 214 $obj->address = "Tel-Aviv"; 215 216 print $obj->id; 217 print "\n"; 218 219 $obj = $obj->__clone(); 220 221 print $obj->id; 222 print "\n"; 223 print $obj->name; 224 print "\n"; 225 print $obj->address; 226 print "\n"; 227?> 228 229Example 8: Unified constructors 230=============================== 231 232<? 233 234 class BaseClass { 235 function __construct() 236 { 237 print "In BaseClass constructor\n"; 238 } 239 } 240 241 class SubClass extends BaseClass { 242 function __construct() 243 { 244 parent::__construct(); 245 print "In SubClass constructor\n"; 246 } 247 } 248 249 $obj = new BaseClass(); 250 251 $obj = new SubClass(); 252 253?> 254 255Example 9: Destructors 256======================= 257 258<?php 259 260class MyDestructableClass { 261 function __construct() 262 { 263 print "In constructor\n"; 264 $this->name = "MyDestructableClass"; 265 } 266 267 function __destruct() 268 { 269 print "Destroying " . $this->name . "\n"; 270 } 271} 272 273$obj = new MyDestructableClass(); 274 275?> 276