Created
June 11, 2013 21:16
-
-
Save dongilbert/5760779 to your computer and use it in GitHub Desktop.
PHP Scope Issues? or is this expected? I don't understand why the `$this` in `Foo::bar()` is being treated as though it is referencing the `$bar` instance. If I declare `Foo::bar()` as static, then I get the expected error of `Using $this when not in object context.` How can you call a method from another object if `$this` in that object refers …
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 Foo | |
{ | |
public function bar() | |
{ | |
return $this->baz(); | |
} | |
public function baz() | |
{ | |
return 'buz'; | |
} | |
} | |
class Bar | |
{ | |
public function test() | |
{ | |
return Foo::bar(); | |
} | |
} | |
$bar = new Bar; | |
echo $bar->test(); | |
?> | |
Outputs the following: | |
PHP Fatal error: Call to undefined method Bar::baz() in php shell code on line 1 |
I ended up doing something similar to your second suggestion to fix it. I just thought it was very strange that $this
within Foo::bar()
was referencing the $bar
instance.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I came across this scenario when debugging some old code, and couldn't figure out why I was getting the "Undefined method" error. No where that I could trace was calling the method in question. I stumbled upon the bug where the code followed this scenario where an instantiated class was calling another class method statically.