Skip to content

Instantly share code, notes, and snippets.

@andrewminton
Forked from bxt/private_public_test.php
Created January 18, 2013 11:49
Show Gist options
  • Save andrewminton/4564125 to your computer and use it in GitHub Desktop.
Save andrewminton/4564125 to your computer and use it in GitHub Desktop.

Revisions

  1. @bxt bxt revised this gist Mar 4, 2011. 1 changed file with 68 additions and 0 deletions.
    68 changes: 68 additions & 0 deletions private_public_test_static.php
    Original 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.
    */
  2. @bxt bxt revised this gist Mar 4, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion private_public_test.php
    Original 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 overridden properties here.
    Notice that I do use overridden properties here.
    You could not access $foo from B if it was not overwritten there.
    */
  3. @bxt bxt revised this gist Mar 4, 2011. 2 changed files with 10 additions and 5 deletions.
    9 changes: 6 additions & 3 deletions private_public_test.php
    Original 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 ust like private ones.
    Notice that I do overridden properties here. You could not access $foo from B if it was not overwritten there.
    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.
    */
    6 changes: 4 additions & 2 deletions private_public_test_methods.php
    Original 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 17"
    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"
    */
  4. @bxt bxt revised this gist Mar 4, 2011. 2 changed files with 40 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions private_public_test.php
    Original 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 owerwritten ust like private ones.
    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.
    */


    */
    38 changes: 38 additions & 0 deletions private_public_test_methods.php
    Original 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"
    */
  5. @bxt bxt created this gist Mar 4, 2011.
    32 changes: 32 additions & 0 deletions private_public_test.php
    Original 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.
    */


    */