Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use __Call magic function in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 316
    Comment on it

    Hellow Reader's !, Before going to Discuss about __Call Function in PHP u should know about Magic Function in PHP. If you have ever looked at the source code of open source PHP projects, you might have noticed object methods that begin with a double underscore.
    These are Magic Methods that allow you to react to certain events when using these particular objects. This means when certain things happen to your object, you can define how it should react in that instance.

     

    Now we will discuss about __CAll Method.

    There are actually two methods which are similar enough that they don't get their own title in this post! The first is the __call method, which gets called, if defined, when an undefined method is called on this object. The second is __callStatic which behaves in exactly the same way but responds to undefined static method calls instead (this was added in PHP 5.3). Probably the most common thing I use __call for is polite error handling, and this is especially useful in library code where other people might need to be integrating with your methods.

    So for example if a script had a Penguin object called $penguin and it contained $penguin->speak() ... the speak() method isn't defined so under normal circumstances we'd see:

    PHP Fatal error: Call to undefined method Penguin::speak() in ...
    What we can do is add something to cope more nicely with this kind of failure than the PHP fatal error you see here, by declaring a method __call.

     

    For example:

    class Animal {
    }
    class Penguin extends Animal {
    
      public function __construct($id) {
        $this->getPenguinFromDb($id);
      }
    
      public function getPenguinFromDb($id) {
        // elegant and robust database code goes here
      }
    
      public function __get($field) {
        if($field == 'name') {
          return $this->pname;
        }
      }
    
      public function __set($field, $value) {
        if($field == 'name') {
          $this->pname = $value;
        }
      }
    
      public function __call($method, $args) {
          echo "unknown method " . $method;
          return false;
      }
    }

    This will catch the error and echo it. In a practical application it might be more appropriate to log a message, redirect a user, or throw an exception, depending on what you are working on - but the concept is the same. Any misdirected method calls can be handled here however you need to, you can detect the name of the method and respond differently accordingly - for example you could handle method renaming in a similar way to how we handled the property renaming above.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: