Chaining Variables
Do you have always asked yourself how to chain variables to make a sentence?
In the magic __get()-method the variable names will be collected, and finally the echo “calls” the __toString()-method.
<?php
class Sentence {
protected $_words = array();
public static function create() {
return new self;
}
public function __get($word) {
$this->_words[] = $word;
return $this;
}
public function __toString() {
return implode(' ', $this->_words) . '.';
}
}
Usage:
echo Sentence::create()->All->your->base->are->belong->to->us;
Output:
All your base are belong to us.
Kategorienphp