Studyon Minte9.com
ZCE 5.3

Study

PHP 5 & SPL



	// Magic methods - __get is called when a variable class is undefined
	// Type hinting - function test(myOtherClass $moc); cannot be used with integer and scalar
	// Pass by reference - all objects are pass by reference
	// Constants - can now be defined within class
	// PPP - protected accessed from current class and descendants
	// Interface - define common Api; no body
	// Abstract - models for class; blueprints; signature and body
	// Extensions  - PDO; SPL; Reflexion
	// SPL - Standart PHP Library
	// Predifined classes: ArrayObject
	// Predifined interfaces: Countable, Serializable, ArrayAccess, Iterator


PHP 5 magic methods

With __get() method you can keep track of variables which are not defined inside the class.

<?php class myController { private $_modelBroker = array(); public function __get($var) { if (strstr($var, 'model')) { $modelKey = substr($var, 5); // variable is already registered or not if (in_array($modelKey, array_keys($this->_modelBroker))) { return $this->_modelBroker[$modelKey]; } else { eval('$model = new Model_'.$modelKey.'();'); $this->_modelBroker[$modelKey] = $model; return $model; } } } } class Model_Number { public $id = 123; } $controller = new myController(); echo $controller->modelNumber->id; // Output 123 // --- Look Here --- // // __get is called, because modelNumber is undefined
  PHP 5 introduces type hinting (only for objects)
<?php class myClass { public function test(myOtherClass $myotherclass) { // --- Look Here --- // echo $myotherclass->var; } } class myOtherClass { public $var = "Hello World"; } $mc = new myClass(); $moc = new myOtherClass(); $mc->test($moc); // output: Hello World $mc->test(); // Catchable fatal error: Argument 1 passed to myClass::test() // must be an instance of myOtherClass, none given
  Type hints can not be used with scalar types such as int or string.
<?php class myClass { public function test(int $number) { echo $number; } } $mc = new myClass(); $a = (int) 10; $mc->test($a); // --- Look Here --- // // Catchable fatal error: Argument 1 passed to myClass::test() // must be an instance of int, integer given
  PHP 5
All objects in PHP 5 are passed by reference. Constants, can now be defined within the scope of a class. Class methods and properties can now be declared as public, private or protected. Classes and methods can also be declared as final to prevent further inheritance. Interfaces can be used to define common APIs. Abstract classes provide models for class implementations that follow a specific blueprint.
PHP 5 - New extensions
PHP Data Objects extension provides a unified DB access extension. Standard PHP Library (SPL) provides numerous interfaces. Reflection Allows for runtime introspection of executing PHP code.
SPL - Predefined Classes ArrayObject allows objects to work as arrays Implements IteratorAggregate , Traversable , ArrayAccess , Serializable , Countable
<?php $array= array('a','b','c'); $obj = new ArrayObject($array); $obj->append('d'); // --- Look Here --- // echo $obj->count(); // Output: 4 $iterator = $obj->getIterator(); while($iterator->valid()) { echo $iterator->current(); $iterator->next(); // --- Look Here --- // } // Output: abcd
  SPL - Predefined Interfaces SPL - Countable
<?php // Example 1 (bad) class A { protected $myCount = 3; public function count() { return $this->myCount; } } $a = new A(); echo $a->count(); // Output: 3 echo count($a); // Output: 1 !! // --- Look Here --- // // Example 2 (good) class B implements Countable { protected $myCount = 3; public function count() { return $this->myCount; } } $b = new B(); echo $b->count(); // Output: 3 echo count($b); // Output: 3 // --- Look Here --- //
  SPL - Serializable
<?php // Interface for customized serializing class Obj implements Serializable { private $data; public function __construct() { $this->data = "My private data"; } public function serialize() { return serialize($this->data); } public function unserialize($str) { return unserialize($str); } } $obj = new Obj(); echo $str = $obj->serialize(); // Output: s:15:"My private data"; // --- Look Here --- // echo $obj->unserialize($str); // My private data
  SPL - ArrayAccess
<?php // Interface to provide accessing objects as arrays class myArray implements ArrayAccess { protected $array = array(); public function offsetExists($offset) { return array_key_exists($this->array[$offset]); } public function offsetGet($offset) { return $this->array[$offset]; } public function offsetSet($offset, $value) { if (!is_numeric($offset)) { // --- Look Here --- // throw new Exception ("Invalid Offset!"); } $this->array[$offset] = $value; } public function offsetUnset($offset) { unset($this->array[$offset]); } } $ma = new myArray(); $ma[1] = 2; // Works $ma['a'] = 3; // Throws exception // --- Look Here --- //
  SPL - Iterator
<?php // Interface for external iterators or objects that can be iterated themselves internally class myArray implements Iterator { private $_data = array('foo', 'bar', 'baz'); private $_current = 0; public function current() { return $this->_data[$this->_current]; } public function next() { $this->_current += 1; } public function key() { return $this->_current; } public function valid() { return isset($this->_data[$this->_current]); } public function rewind() { $this->_current = 0; } } $ma = new myArray(); echo $ma->valid(); // Output: 1 echo $ma->current(); // Ouput: foo $ma->next(); $ma->next(); echo $ma->key(); // Ouput: 2 $ma->rewind(); while($ma->valid()) { echo $ma->current(); $ma->next(); } // Output: foobarbaz
  SPL - SeekableIterator
<?php interface SeekableIterator { function current(); function next(); function key(); function rewind(); function valid(); function seek($index); }