Search code examples
phpphp-5.3

Call to undefined function handler


Is it possible to handle this type of errors? Something like spl_autoload_register, but for functions.

Basically, what I am trying to do:

I have the class:

class Foo {
   public function bar() {
     echo 1;
   } 
}

So, when I call a nonexistent function Foo() like this:

Foo()->bar();

The possible handler should create a function Foo(), which looks like that:

function Foo() {
   return new Foo();
}

Solution

  • If you never need an actual instance of the object, why not use a static class?

    class Foo {
       public static function bar() {
         echo 1;
       } 
    }
    
    Foo::bar();
    

    Then, you can do this in your app:

    $this->fiends = FriendsModel::getUserFriends($userId);