Guest User

Covariance testing

a guest
Jul 25th, 2013
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class Base
  3. {
  4.     protected $foo = null;
  5.     final public function __construct(Granny $init = null)
  6.     {
  7.         return $this->init($init);
  8.     }
  9.     protected function init(Granny $dependency = null)
  10.     {
  11.         throw new Exception("Don't run me from Base, foo!");
  12.     }
  13. }
  14. class Granny
  15. {
  16.     protected $name = null;
  17.     protected $age = null;
  18.     public function __construct(array $vals = null)
  19.     {
  20.         foreach($vals as $name => $val)
  21.         {
  22.             $name = 'set'.ucfirst($name);
  23.             if (method_exists($this, $name))
  24.             {
  25.                 $this->{$name}($val);
  26.             }
  27.         }
  28.         return $this;
  29.     }
  30.     //basic gettter && setters
  31.     public function getAge()
  32.     {
  33.         return $this->age;
  34.     }
  35.     public function setAge($age = null)
  36.     {
  37.         $this->age = $age === null ? null : (int) $age;
  38.         return $this;
  39.     }
  40. }
  41.  
  42. class Dad extends Granny
  43. {
  44.     //Dad's secret
  45.     private $likesMom = null;
  46.     public function getLikesMom()
  47.     {
  48.         return $this->likesMom;
  49.     }
  50.     public function setLikesMom($bool = null)
  51.     {
  52.         $this->likesMom = $bool === null ? null : !!$bool;
  53.         return $this;
  54.     }
  55. }
  56.  
  57. class Ball extends Base
  58. {
  59.     protected function init(Dad $dependency = null)
  60.     {
  61.         $this->foo = $dependency;
  62.         return $this;
  63.     }
  64. }
  65.  
  66. var_dump(new Ball(new Dad()));
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×