PHP5 Magic Functions

PHP 5 have a new set of Magic methods for it’s object that can make the life of any developer easy. Let’s take a look on some of them.

  • __autoload($className) : This method, from all of them, is by far the best. No more need of including classes declarations on every document. Now by only calling the class name, the __autoload method will automaticly create a object based on the class name that you have given. The best way to work this around is by setting the class name and the file name as the same, but if you don’t want to, just do the right logic inside the method.
    function __autoload($classname) { include_once “{$classname}.php”; }
    P.S. This method usually is created on the header of your application, not inside a class.
  • __get($what) and __set($what,$whitValue): No more declaring one get and one set for each variable and no more need to call get() or set() when needing to get or set a variable. Declaring the __get and __set magic methods will allow you to set or get a variable even if it is protected or private as it is a public variable. Consider that you have a protected $id on your class that sometimes you need to get it. You can declare a method getId() to retrieve this value or changing it to public. How about you keep as it is and declare a __get method on the class and reference the id as a public variable? Well this is whole concept of the __get method. Once you have declared you can access the value of the variable as it is public.
    class tmpObject {
    protected $id;
    # all methods of the class
    __get($what) {
    if(isset($this->$what)) {
    return $this->$what;
    } # class end

    $obj = new testObject();
    # id is a protected member of the object

  • __call() method: This works similar to the __get() method, but for functions. When well defined it will call the correct function that you have referenced on the class and retrieve return. The best use for this method is to display a friendly error message when a function does not exists inside the class scope.
  • __toString() method: This is a way to display your object information as a string, so whenever you echo or print it, this method will return a string value (whatever you have defined) for it to print.
  • __clone() method: Well this a tricky … but let’s keep the concept simple. If you need to make a copy of a object and need it work in a different way for any reason, you must declare the __clone method with the correct logic. There’s a great example of how you can use it at PHP.NET Object Clonning Manual.

About mcloide

Making things simpler, just check: View all posts by mcloide

2 responses to “PHP5 Magic Functions

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: