Created
January 22, 2012 13:14
-
-
Save eteubert/1657038 to your computer and use it in GitHub Desktop.
PHP Weirdness: Calling Non-Static Methods Statically
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 characters
| <?php | |
| class A { | |
| public function foo() { | |
| echo "Hello World"; | |
| } | |
| } | |
| A::foo(); // => Hello World |
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 characters
| <?php | |
| class A { | |
| public $bar = 42; | |
| public function foo() { | |
| echo $this->bar; | |
| } | |
| } | |
| A::foo(); // => Fatal error: Using $this when not in object context in /code/Yk6d7i on line 5 |
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 characters
| <?php | |
| class A { | |
| public $bar = 42; | |
| public function foo() { | |
| echo $this->bar; | |
| } | |
| } | |
| class B { | |
| public $bar = 23; | |
| public function __construct() { | |
| A::foo(); | |
| } | |
| } | |
| new B; // => 23 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment