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