Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error thrown when spying on static #119

Open
ciaranmcnulty opened this issue Aug 29, 2014 · 2 comments
Open

Fatal error thrown when spying on static #119

ciaranmcnulty opened this issue Aug 29, 2014 · 2 comments

Comments

@ciaranmcnulty
Copy link
Member

This is an invalid case but should be handled better

Spec:

class SpiesExampleSpec extends ObjectBehavior
{
    function it_errors_when_doubling_statics(\DateTime $time)
    {
        $this->getErrorsFromTime($time);

        // getLastErrors is static!
        $time->getLastErrors()->shouldHaveBeenCalled();
    }
}

Object:

class SpiesExample
{
    public function getErrorsFromTime(\DateTime $time)
    {
        $time->getLastErrors();
    }
}

Output:

PHP Fatal error:  Using $this when not in object context in /Users/cmcnulty/spies-error/vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/ClassCreator.php(49) : eval()'d code on line 17
@ciaranmcnulty
Copy link
Member Author

I guess what I'd like to happen is for prophecy to disallow invoking the message with a sensible error, but I'm not sure if that stops any legitimate use-cases?

@hanneskaeufler
Copy link
Contributor

I just run into this, get the same error. PHP allows us to call static methods from instances of the class and considers this a valid case? See http://php.net/manual/en/language.oop5.static.php. A property declared as static cannot be accessed with an instantiated class object **(though a static method can)**.

This would be useful to be able to inject and Factory type classes.

class FooFactory {
    public static create() { return new Foo(); }
}

class SystemUnderTest {
    public function __construct(FooFactory $factory) {
        // ...
    }

    public function doStuff() {
        $foo = $this->factory->create();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants