Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Magic functions in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 271
    Comment on it

    Hello readers today we discussed about "Magic Functions in PHP".

    Have you ever looked at the source code of open source PHP projects, you could have observed object methods that commence with a double underscore.

    These are Magic Function that allow you to react to certain occasions when using these particular objects. This implies when certain things occur to your thing, you can define how it should react in that instance.

    You've probably already come across a number of PHP's special methods already because a few of them are really quite common. Nevertheless, I really believe in order to be a competent PHP developer, you do desire a firm grasp of the tools you have at your disposal when working with Object Oriented PHP.

    Magic methods in php couple of predefined function by php compiler which executes on some event. Magic methods starts with prefix __, for example __call, __get, __set. I am including magic methods topic in my oop tutorial here because these method mainly applied in classes of PHP.  __construct is a magic method which automatically call on creating thing of the classes. Presently there are vatious magic methods in php.

    List Of magic functions:

    __construct()
    __destruct()
    __call()
    __callStatic()
    __get()
    __set()
    __isset()
    __unset()
    __sleep()
    __wakeup()
    __toString()
    __invoke()
    __set_state()
    __clone()
    __debugInfo() 

    Here is the basic example of magic function.

    class hello {
    	function __construct() {
    		echo 1;
    	}
    
    	function __destruct() {
    		echo 2;
    	}
    }
    $object = new hello(); //__construct get automatically executed and print 1 on screen
    unset($object);//__destruct triggers and print 2.

     

 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: