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 |
This code just shouldn't work. You can't call an instance method $this->baz()
if you don't have a Foo instance.
Some solutions
<?php
class Foo
{
public static function bar()
{
return static::baz();
}
public static function baz()
{
return 'buz';
}
}
class Bar
{
public function test()
{
return Foo::bar();
}
}
$bar = new Bar;
echo $bar->test();
and
<?php
class Foo
{
public function bar()
{
return $this->baz();
}
public function baz()
{
return 'buz';
}
}
class Bar
{
public function test()
{
$f = new Foo;
return $f->bar();
}
}
$bar = new Bar;
echo $bar->test();
Or PHP 5.4
<?php
class Foo
{
public function bar()
{
return $this->baz();
}
public function baz()
{
return 'buz';
}
}
class Bar
{
public function test()
{
return (new Foo)->bar();
}
}
$bar = new Bar;
echo $bar->test();
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.
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'm just stupid, that's the answer.
For the obvious one, you shouldn't be calling a non-static method statically.
What I don't get though is how the
$this
inFoo::bar
is referencing the calling object.