PHP Magic Functions: Best Part of Object Oriented PHP – Part 1


There are some reserved function names  in PHP class starting with __ ( double underscore ). These are __construct, __destruct, __isset, __unset, __call, __callStatic, __sleep, __wakeup, __get, __set, __toString, __set_state, __invoke and __clone. You cannot use these functions to serve your logical purpose but these are meant to be used for providing magic functionality.

Lets start with the most familiar ones …

__construct and __descruct : Play with birth ( initialization ) and death ( destroy ) of an object

These methods are better known as constructor ( called when object is initialized ) and destructor ( called when object is destroyed or scope of an object vanishes ), well known keywords in Object Oriented Programming. Most of you guys are already familiar with these functions therefore I am not going to discuss them in detail.

__call and __callStatic : Don’t leave any undefined function in your class

There are two ways of calling a class function. First one is with Object Scope and second one is with Static Scope. In former case an object of the class is first instantiated and then a function is called using the -> ( I’m missing its name, any suggestion !!! ) operator while in latter case the function is called using the Scope Resolution Operator( :: ) with class name.

Whenever you try to access a function of your class with object scope which is not defined it throws an error. e. g.

1
2
3
4
5
6
7
<?php
  class myClass() {
    private $a = true;
  }
 
  $myObj = new myClass();
  $myObj->showValue();

This will throw an error on execution:

Fatal error: Call to undefined method myClass::showValue() in <path-to-your-file>.php on line 11

To avoid this you can use magic function __call, this function is called whenever an undefined function of a class is called with object scope. Lets have a look

1
2
3
4
5
6
7
8
9
10
<?php
  class Greetings {
    function __call( $functionName, $argumentsArray ) {
      echo "Hello, " . ucfirst( $functionName ) . " !!!";
    }
  }
  $sayHello = new Greetings();
  $sayHello->steve();
  $sayHello->adams();
  $sayHello->dexter();

The output of the above code will be :

1
2
3
Hello Steve !!!
Hello Adams !!!
Hello Dexter !!!

Now think what will happen, if we call an undefined function with static scope, this will again throw an error on execution. To handle call to undefined static functions the __callStatic magic function is used.

1
2
3
4
5
6
7
8
9
10
<?php
  class Greetings {
    static function __callStatic( $functionName, $argumentsArray ) {
      echo "Hello, " . ucfirst( $functionName ) . " !!!";
    }
  }
 
  Greetings::steve();
  Greetings::adams();
  Greetings::dexter();

The output of the above code will be same as it was for the former code :

1
2
3
Hello Steve !!!
Hello Adams !!!
Hello Dexter !!!

For rest of the magic functions keep visiting my blog and don’t forget to leave feedback about the the post.

Happy coding and keep it as simple as possible.

Recently Published

»

How to scroll to the top of a webpage using jQuery ?

If you are looking for an extraordinary method of smoothly scrolling ...

»

11 sources to gain perfect knowledge of html5 semantic tags

1. Html5tuts Html5tuts is a site dedicated to HTML5. Feel free to ...

»

How to move an element into another parent element using jQuery?

An important piece to the puzzle when coding with jQuery is that how ...

»

10 optionally Good WordPress Typography Plugins You could Opt For

It is not necessary that the person will follow the standard guide ...

»

8 creative 3d slider wordpress themes to keep your blog active

1. Display Display is a WordPress Theme best used for Portfolio and ...

»

11 quick remote screen sharing apps worth trying

1. Join Me Easy to start, easy to join, easy to manage, join.me takes ...

»

9 perfectly adsense optimized seo friendly responsive magazine wordpress themes

1. Simple Life SIMPLE LIFE is a WordPress responsive theme with a ...

»

9 out of the box jquery plugins not to miss

1. Hatchshow Hatchshow is a typesetting plugin for jQuery that ...

»

10 fresh libraries and web apps you can’t afford to miss

1. Pagekit Pagekit gives you the tools to create beautiful websites. ...