3

I know there are other ways around this but just for the sake of simplicity I would like to know if its possible to do something like:

   MyFactory::Create()->callMe();

and myFactory class:

   class MyFactory {
       private $obj;

       public static Create() { 
           $this->obj = new ClassA();
           return $this->obj;
       }

       public function __undefinedFunctionCall() {
           //function does not exist within MyFactory Class but it exists
           // in ClassA
           $this->obj->callMe();
       }
   }

so basically the function callMe does not exist within MyFactory class but it exists in ClassA. Dont ask me why I cant just extend classA because the code structure is already written and its not up to me to modify it. I just need a way around it.

2
  • Looks like a case for __call() Commented Sep 4, 2013 at 16:32
  • I think thats it inside __call I could check if function exists and if not try the function from other class. thanks Mark Commented Sep 4, 2013 at 16:34

4 Answers 4

5

You could simply use method_exists

It takes 2 parameters method_exists ( mixed $object, string $method_name )

The first is an object instance or a class name, and the second is The method name.

So it should be as simple as:

if(method_exists($this->obj,'callMe')){
   $this->obj->callMe();
}
else{
//...throw error do some logic
}
Sign up to request clarification or add additional context in comments.

1 Comment

as I mentioned i do not want to do this check outside of class. Want my Factory class to detect any undefined function calls
1

Thanks to Mark Baker I found the solution. In case anyone else has similar problem the following solved it:

added this function inside my factory class

    public function __call($name, $arguments)
    {
            //if function exists within this class call it
        if (method_exists(self, $name))
        {
            $this->$name($arguments);
        }
        else
        {
            //otherwise check if function exists in obj and call it
            if (method_exists($this->obj, $name))
            {
                $this->obj->$name($arguments);
            }
            else 
            {
                throw new \Exception('Undefined function call.');
            }          
        }
    }

1 Comment

Also notice that you can not use $this inside static Create method as you wrote in the question.
0

To checks if the class method exists or not:

$this->obj = new ClassA();
var_dump(method_exists($this->obj,'callMe'));

if it is static method:

var_dump(method_exists('ClassA','callMe'));

Returns true if the method given by method_name has been defined for the given object, false otherwise.

php.net manual

1 Comment

that I know but I want to detect it within class inside MyFactory and not outside of it.
0

Your correct about using the Magic method __call(), but you are implementing it wrong.

  1. You dont need to check if it exists on its own class, PHP does this internally for you. The magic method __call only happens if the method doesn't exist.
  2. You are passing a single array argument to $this->obj->$name, where you should be passing in each argument separately.
  3. Nothing gets returned.

This is how you should implement the __call magic method for your purpose.

public function __call($name, $arguments) {
    if(method_exists($this->obj,$name)) {
        return call_user_func_array(array($this->obj, $name), $arguments);
    } else {
        throw new Exception("Undefined method ".get_class($this)."::".$name);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.