Memory App
Programming
 




ArrayObject

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');
    echo $obj->count();
        // Output: 4

    $iterator = $obj->getIterator();
    while($iterator->valid()) {
        echo $iterator->current();
        $iterator->next();
    }
        // Output: abcd
(1/6)

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 !!

    
    // 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
(2/6)

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";

    echo $obj->unserialize($str);
        // My private data
(3/6)

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)) {
                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
(4/6)

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
(5/6)

SeekableIterator


<?php
    interface SeekableIterator {
        
        function current();
        function next();
        function key();
        function rewind();
        function valid();

        function seek($index);
    }


Questions



Top Reference > Programming
0/0 (80)  
Not Logged