Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Created June 11, 2013 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongilbert/5760779 to your computer and use it in GitHub Desktop.
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 …
<?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
@dongilbert
Copy link
Author

🤦 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 in Foo::bar is referencing the calling object.

@zackkitzmiller
Copy link

This code just shouldn't work. You can't call an instance method $this->baz() if you don't have a Foo instance.

@zackkitzmiller
Copy link

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();

@zackkitzmiller
Copy link

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();

@zackkitzmiller
Copy link

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();

@dongilbert
Copy link
Author

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.

@dongilbert
Copy link
Author

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