design patterns - detect if function exists within the class and if not call a different function. PHP -


i know there other ways around sake of simplicity know if possible 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 not exist within myfactory class exists            // in classa            $this->obj->callme();        }    } 

so function callme not exist within myfactory class exists in classa. dont ask me why cant extend classa because code structure written , not me modify it. need way around it.

you use method_exists

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

the first object instance or class name, , second method name.

so should simple as:

if(method_exists($this->obj,'callme')){    $this->obj->callme(); } else{ //...throw error logic } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -