MemoryRefresh!

Various / SPL   SPL



Questions 1 Answers 0%

Pages   0 from 91
Questions   0 from 142

Reset


SPL - Predefined Classes ArrayObject allows arrays to work as objects. 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); }

Related Pages

SPL
XML
SOAP
JSON
PHPUNIT
PHAR


0% 100%
 
 
0 pages 91 pages
1) What interface do you need to implement with the following code? <?php class myClass implements ____ { } $obj = new myClass(); $str = $obj->serialize();





2) How can you use an array as object?








SPL - Predefined Classes ArrayObject allows arrays to work as objects. 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); }

References









Programming

Php
   
Regex
   
MySQL
   
Css
   
jQuery
   
Git
   


References