Skip to content

Instantly share code, notes, and snippets.

@ManInTheBox
Created March 30, 2017 11:40
Show Gist options
  • Save ManInTheBox/cbb47bb3bd6b3762013d36ab81a010a0 to your computer and use it in GitHub Desktop.
Save ManInTheBox/cbb47bb3bd6b3762013d36ab81a010a0 to your computer and use it in GitHub Desktop.
Do not use static property inheritance. It's a class level inheritance, therefore "global".
Psy Shell v0.7.2 (PHP 7.1.0 — cli) by Justin Hileman
>>> class StaticParent
... {
... public static $catchAll = 'this is initial value';
... }
=> null
>>> class ChildOne extends StaticParent
... {}
=> null
>>> class ChildTwo extends StaticParent
... {}
=> null
>>> class ChildThree extends StaticParent
... {}
=> null
>>> $one = new ChildOne();
=> ChildOne {#180}
>>> $two = new ChildTwo();
=> ChildTwo {#184}
>>> $three = new ChildThree();
=> ChildThree {#186}
>>> $one::$catchAll = 'hi this is for child ONE';
=> "hi this is for child ONE"
>>> $two::$catchAll = 'well, I am child TWO';
=> "well, I am child TWO"
>>> $three::$catchAll = 'nice try folks, but static property breaks encapsulation. you are all doomed';
=> "nice try folks, but static property breaks encapsulation. you are all doomed"
>>> var_dump($one::$catchAll);
string(76) "nice try folks, but static property breaks encapsulation. you are all doomed"
=> null
>>> var_dump($two::$catchAll);
string(76) "nice try folks, but static property breaks encapsulation. you are all doomed"
=> null
>>> var_dump($three::$catchAll);
string(76) "nice try folks, but static property breaks encapsulation. you are all doomed"
=> null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment