1 Example 1: A singleton (static members)
2 =======================================
9 function increment_and_print()
11 print ++$this->counter;
17 class SingletonCounter {
18 static $m_instance = NULL;
22 if (self::$m_instance == NULL) {
23 self::$m_instance = new Counter();
25 return self::$m_instance;
29 SingletonCounter::Instance()->increment_and_print();
30 SingletonCounter::Instance()->increment_and_print();
31 SingletonCounter::Instance()->increment_and_print();
35 Example 2: Factory method (derefencing objects returned from functions)
36 =======================================================================
54 function ShapeFactoryMethod($shape)
64 ShapeFactoryMethod("Circle")->draw();
65 ShapeFactoryMethod("Square")->draw();
70 Example 3: Class constants and class scope
71 ==========================================
76 const FATAL = "Fatal error\n";
77 const WARNING = "Warning\n";
78 const INFO = "Informational message\n";
80 function print_fatal_error_codes()
82 print "FATAL = " . FATAL;
83 print "self::FATAL = " . self::FATAL;
87 /* Call the static function and move into the ErrorCodes scope */
88 ErrorCodes::print_fatal_error_codes();
92 Example 4: Regular object method using both local and global functions
93 ======================================================================
98 const HELLO_WORLD = "Hello, World!\n";
100 function get_hello_world()
105 function length_of_hello_world()
107 $str = $this->get_hello_world();
112 $obj = new HelloWorld();
113 print "length_of_hello_world() = " . $obj->length_of_hello_world();
117 Example 5: Multiple derefencing of objects returned from methods
118 ================================================================
124 function Name($_name)
126 $this->name = $_name;
137 function Person($_name, $_address)
139 $this->name = new Name($_name);
148 $person = new Person("John", "New York");
149 $person->getName()->display();
153 Example 6: Exception handling
154 =============================
158 function MyException($_error) {
159 $this->error = $_error;
162 function getException()
168 function ThrowException()
170 throw new MyException("'This is an exception!'");
175 } catch (MyException $exception) {
176 print "There was an exception: " . $exception->getException();
182 } catch (MyException $exception) {
183 print "There was an exception: " . $exception->getException();
196 function MyCloneable()
198 $this->id = self::$id++;
203 $this->name = $that->name;
204 $this->address = "New York";
205 $this->id = self::$id++;
211 $obj = new MyCloneable();
213 $obj->name = "Hello";
214 $obj->address = "Tel-Aviv";
219 $obj = $obj->__clone();
229 Example 8: Unified constructors
230 ===============================
235 function __construct()
237 print "In BaseClass constructor\n";
241 class SubClass extends BaseClass {
242 function __construct()
244 parent::__construct();
245 print "In SubClass constructor\n";
249 $obj = new BaseClass();
251 $obj = new SubClass();
255 Example 9: Destructors
256 =======================
260 class MyDestructableClass {
261 function __construct()
263 print "In constructor\n";
264 $this->name = "MyDestructableClass";
267 function __destruct()
269 print "Destroying " . $this->name . "\n";
273 $obj = new MyDestructableClass();