Magic Method of OOPHP

Hello friends

Now I am ready to teach you the magic method in Object Oriented PHP. Now the question is, what is Magic Method? It is a that kind of method by which we can identify the class instant by this Magic Method. When we call a class this magic method inform me about the call functions.

To maintain this class, PHP provide the __construct() method. OK, lets go to write code of Magic Method.

<?php
class myClass{

    public
        $name="Bismillahir Rahmanir Rahim";

    function __construct()
    {
        echo 'The class is ',__CLASS__,'.<br>';    
    }

    function get_property()
    {
        return $this->name."<br/>";    
    }

    function set_property($newVal)
    {
        $this->name=$newVal;    
    }

}

    $obj= new myClass;

    echo $obj->get_property();
    $obj->set_property("Allah is Almighty.");
    echo $obj->get_property();

?>

Here, I use a magic method ‘__construct()’ to know about the class name. OK, our output is like that install.

The class is myClass.
Bismillahir Rahmanir Rahim
Allah is Almighty.

Ok, that’s finished. Thanks for reading. Wait for next tutorial.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s