The ArrayObject class
(PHP 5, PHP 7)
This class allows objects to work as arrays.
类摘要
ArrayObject implements IteratorAggregate , ArrayAccess , Serializable , Countable
{
/* 常量 */
const integer STD_PROP_LIST = 1 ;
const integer ARRAY_AS_PROPS = 2 ;
/* 方法 */
public __construct ([ mixed $input = array() [, int $flags = 0 [, string $iterator_class = "ArrayIterator" ]]] )
public append ( mixed $value ) : void
public asort ( void ) : void
public count ( void ) : int
public exchangeArray ( mixed $input ) : array
public getArrayCopy ( void ) : array
public getFlags ( void ) : int
public getIterator ( void ) : ArrayIterator
public getIteratorClass ( void ) : string
public ksort ( void ) : void
public natcasesort ( void ) : void
public natsort ( void ) : void
public offsetExists ( mixed $index ) : bool
public offsetGet ( mixed $index ) : mixed
public offsetSet ( mixed $index , mixed $newval ) : void
public offsetUnset ( mixed $index ) : void
public serialize ( void ) : string
public setFlags ( int $flags ) : void
public setIteratorClass ( string $iterator_class ) : void
public uasort ( callable $cmp_function ) : void
public uksort ( callable $cmp_function ) : void
public unserialize ( string $serialized ) : void
}
As you know ArrayObject is not an array so you can't use the built in array functions. Here's a trick around that:
Extend the ArrayObject class with your own and implement this magic method:
<?php
public function __call($func, $argv)
{
if (!is_callable($func) || substr($func, 0, 6) !== 'array_')
{
throw new BadMethodCallException(__CLASS__.'->'.$func);
}
return call_user_func_array($func, array_merge(array($this->getArrayCopy()), $argv));
}
?>
Now you can do this with any array_* function:
<?php
$yourObject->array_keys();
?>
- Don't forget to ommit the first parameter - it's automatic!
Note: You might want to write your own functions if you're working with large sets of data.
The SplObserver interface
(PHP 5 >= 5.1.0, PHP 7)
The SplObserver interface is used alongside SplSubject to implement the Observer Design Pattern.
类摘要
SplObserver
{
/* 方法 */
abstract public update ( SplSubject $subject ) : void
}
<?php
/**
* Subject,that who makes news
*/
class Newspaper implements \SplSubject{
private $name;
private $observers = array();
private $content;
public function __construct($name) {
$this->name = $name;
}
//add observer
public function attach(\SplObserver $observer) {
$this->observers[] = $observer;
}
//remove observer
public function detach(\SplObserver $observer) {
$key = array_search($observer,$this->observers, true);
if($key){
unset($this->observers[$key]);
}
}
//set breakouts news
public function breakOutNews($content) {
$this->content = $content;
$this->notify();
}
public function getContent() {
return $this->content." ({$this->name})";
}
//notify observers(or some of them)
public function notify() {
foreach ($this->observers as $value) {
$value->update($this);
}
}
}
/**
* Observer,that who recieves news
*/
class Reader implements SplObserver{
private $name;
public function __construct($name) {
$this->name = $name;
}
public function update(\SplSubject $subject) {
echo $this->name.' is reading breakout news '.$subject->getContent().'
';
}
}
$newspaper = new Newspaper('Newyork Times');
$allen = new Reader('Allen');
$jim = new Reader('Jim');
$linda = new Reader('Linda');
//add reader
$newspaper->attach($allen);
$newspaper->attach($jim);
$newspaper->attach($linda);
//remove reader
$newspaper->detach($linda);
//set break outs
$newspaper->breakOutNews('USA break down!');
//=====output======
//Allen is reading breakout news USA break down! (Newyork Times)
//Jim is reading breakout news USA break down! (Newyork Times)
The SplSubject interface
(PHP 5 >= 5.1.0, PHP 7)
The SplSubject interface is used alongside SplObserver to implement the Observer Design Pattern.
类摘要
SplSubject
{
/* 方法 */
abstract public attach ( SplObserver $observer ) : void
abstract public detach ( SplObserver $observer ) : void
abstract public notify ( void ) : void
}
<?php
// Example implementation of Observer design pattern:
class MyObserver1 implements SplObserver {
public function update(SplSubject $subject) {
echo __CLASS__ . ' - ' . $subject->getName();
}
}
class MyObserver2 implements SplObserver {
public function update(SplSubject $subject) {
echo __CLASS__ . ' - ' . $subject->getName();
}
}
class MySubject implements SplSubject {
private $_observers;
private $_name;
public function __construct($name) {
$this->_observers = new SplObjectStorage();
$this->_name = $name;
}
public function attach(SplObserver $observer) {
$this->_observers->attach($observer);
}
public function detach(SplObserver $observer) {
$this->_observers->detach($observer);
}
public function notify() {
foreach ($this->_observers as $observer) {
$observer->update($this);
}
}
public function getName() {
return $this->_name;
}
}
$observer1 = new MyObserver1();
$observer2 = new MyObserver2();
$subject = new MySubject("test");
$subject->attach($observer1);
$subject->attach($observer2);
$subject->notify();
/*
will output:
MyObserver1 - test
MyObserver2 - test
*/
$subject->detach($observer2);
$subject->notify();
/*
will output:
MyObserver1 - test
*/
?>
