Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Friday 13 December 2013

Constructor in PHP - OOP

Below PHP script illustrates constructor in PHP. To create constructor in PHP __construct keyword is used. In our example, we pass one string parameter(in our case "Jignesh") while creating object.


Output:
Hello, Jignesh!

Source Code:

<?php

class ConstructDemo 
{
    public $str;

    function __construct($str) 
    {
        $this->str = $str;
    }

    function getHtml()
    {
       return "<html><body>"."Hello, ".$this->str."!"."</body></html>";
    }
}

$obj = new ConstructDemo("Jignesh");
echo $obj->getHtml();

?>

0 comments :

Post a Comment