-
-
Save andrewminton/4564125 to your computer and use it in GitHub Desktop.
Revisions
-
bxt revised this gist
Mar 4, 2011 . 1 changed file with 68 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ <?php abstract class A { private static $foo="a"; protected static $bar="a"; function getAstuff() { return self::$foo.'/'.self::$bar; } function getAstuffLateStatic() { if(get_called_class()=="A") { return static::$foo.'/'.static::$bar; } else { return /*static::$foo*/'X'.'/'.static::$bar; } } } class B extends A { private static $foo="b"; protected static $bar="b"; function getBstuff() { return self::$foo.'/'.self::$bar; } function getBstuffParent() { return /*parent::$foo*/'X'.'/'.parent::$bar; } } $b=new B(); echo 'B::getBstuff: '.B::getBstuff(). "\nB::getAstuff: ".B::getAstuff(). "\nB::getBstuffParent: ".B::getBstuffParent(). "\nB::getAstuffLateStatic: ".B::getAstuffLateStatic(). "\nA::getAstuffLateStatic: ".A::getAstuffLateStatic(). "\n"; /* Output: B::getBstuff: b/b B::getAstuff: a/a B::getBstuffParent: X/a B::getAstuffLateStatic: X/b A::getAstuffLateStatic: a/a Things work similar in static contexts. The difference is we can freely choose between attributes of parent and child class using parent, static and self. When using self, we do always stay in the defining class and we can access our own private attributes. When using parent and static, we can only access protected attributes, accessing the child's class private properties throws this error: PHP Fatal error: Cannot access private property B::$foo in /home/amoebe/Documents/854668/private_public_test_static.php on line 13 And accessing the parent's private stuff this one: PHP Fatal error: Cannot access private property A::$foo in /home/amoebe/Documents/854668/private_public_test_static.php on line 25 Hence private locks access in both directions. */ -
bxt revised this gist
Mar 4, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ function getBstuff() { As you see, methods defined in class A use the private vars of class A even when used on an instance of child class B. Protected attributes however are overridden just like private ones. Notice that I do use overridden properties here. You could not access $foo from B if it was not overwritten there. */ -
bxt revised this gist
Mar 4, 2011 . 2 changed files with 10 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,10 @@ function getBstuff() { getBstuff: b/b getAstuff: a/b As you see, methods defined in class A use the private vars of class A even when used on an instance of child class B. Protected attributes however are overridden just like private ones. Notice that I do overridden properties here. You could not access $foo from B if it was not overwritten there. */ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,6 +33,8 @@ function getBstuff() { getBstuff: b/b getAstuff: a/b It's just the same thing with methods. If I would not override foo() here it would throw an error: "Fatal error: Call to private method A::foo() from context 'B' in /home/burny/privtest.php on line 23" */ -
bxt revised this gist
Mar 4, 2011 . 2 changed files with 40 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,8 +25,6 @@ function getBstuff() { getAstuff: a/b As you see, methods defined in class A use the private vars of class A even when used on an instance of child class B. Protected attributes however are overridden ust like private ones. Notice that I do overridden properties here. You could not access $foo from B if it was not overwritten there. */ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ <?php class A { private function foo() { return "a"; } protected function bar() { return "a"; } function getAstuff() { return $this->foo().'/'.$this->bar(); } } class B extends A { private function foo() { return "b"; } protected function bar() { return "b"; } function getBstuff() { return $this->foo().'/'.$this->bar(); } } $b=new B(); echo 'getBstuff: '.$b->getBstuff()."\ngetAstuff: ".$b->getAstuff()."\n"; /* Output: getBstuff: b/b getAstuff: a/b It's just the same thing with methods. If I would not override foo() here it would throw an error: "Fatal error: Call to private method A::foo() from context 'B' in /home/burny/privtest.php on line 17" */ -
bxt created this gist
Mar 4, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ <?php class A { private $foo="a"; protected $bar="a"; function getAstuff() { return $this->foo.'/'.$this->bar; } } class B extends A { private $foo="b"; protected $bar="b"; function getBstuff() { return $this->foo.'/'.$this->bar; } } $b=new B(); echo 'getBstuff: '.$b->getBstuff()."\ngetAstuff: ".$b->getAstuff()."\n"; /* Output: getBstuff: b/b getAstuff: a/b As you see, methods defined in class A use the private vars of class A even when used on an instance of child class B. Protected attributes however are owerwritten ust like private ones. */ */