Proxying Objects -Another Gotcha #39

Open
royweston opened this Issue Dec 1, 2011 · 1 comment

2 participants

@royweston

Came across this style of code. Proxying an object that is used in a 'method_exists' call breaks the code as the method is not defined on the proxy object. A simple but not so straight forward answer would be to change all the code to use type hinting.

class AClass
{
    public function aMethod()
    {
        return 3;
    }
}

class BClass
{
    public function __construct($aClass) {
        if (!$aClass || !is_object($aClass) || !method_exists($aClass, "aMethod")) {
            throw new Exception("Invalid AClass object passed to ".__METHOD__);
        }
    // ...
    }
}

$stubAClass = \Mockery::mock('overload:AClass', array('aMethod' => null));
$bClass = new BClass($stubAClass);
@davedevelopment
Collaborator

I've run in to this, is is_callable a viable alternative to method_exists?

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