Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Base
- {
- protected $foo = null;
- final public function __construct(Granny $init = null)
- {
- return $this->init($init);
- }
- protected function init(Granny $dependency = null)
- {
- throw new Exception("Don't run me from Base, foo!");
- }
- }
- class Granny
- {
- protected $name = null;
- protected $age = null;
- public function __construct(array $vals = null)
- {
- foreach($vals as $name => $val)
- {
- $name = 'set'.ucfirst($name);
- if (method_exists($this, $name))
- {
- $this->{$name}($val);
- }
- }
- return $this;
- }
- //basic gettter && setters
- public function getAge()
- {
- return $this->age;
- }
- public function setAge($age = null)
- {
- $this->age = $age === null ? null : (int) $age;
- return $this;
- }
- }
- class Dad extends Granny
- {
- //Dad's secret
- private $likesMom = null;
- public function getLikesMom()
- {
- return $this->likesMom;
- }
- public function setLikesMom($bool = null)
- {
- $this->likesMom = $bool === null ? null : !!$bool;
- return $this;
- }
- }
- class Ball extends Base
- {
- protected function init(Dad $dependency = null)
- {
- $this->foo = $dependency;
- return $this;
- }
- }
- var_dump(new Ball(new Dad()));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement