0

Here is the code of registry I'd like to use. But static doesn't work as it should. In this example it always returns 2 (while 1 is expected). What can it be?

<?php

class CommonRegistry{

    protected static $register;

    public static function show()
    {
        return static::$register;
    }



}

class NewRegister extends CommonRegistry{

    public function __construct($num)
    {
        static::$register = $num;
    }


}

class AnotherRegister extends CommonRegistry
{

    public function __construct($num)
    {
        static::$register = $num;
    }

}


$a = new NewRegister(1);
$b = new AnotherRegister(2);

var_dump(NewRegister::show());
Alok Patel
  • 7,842
  • 5
  • 31
  • 47
Toletov
  • 55
  • 7
  • These examples don't even make sense to use statics imo. Also, sounds like it's working as expected to me. – Jonnix Sep 08 '16 at 11:22
  • It is working as expected. It should be 2 only when you don't override the variable. – Alok Patel Sep 08 '16 at 11:27
  • I refers to the NewRegister::$register when use __constructor, so I thought it should be 1 for NewRegister::show() and 2 for AnotherRegister::show() – Toletov Sep 08 '16 at 11:34
  • Here is a free hint: stop using a static registry. It's an antipattern. – tereško Sep 09 '16 at 15:42

0 Answers0