Friday, 20 October 2017

Constructor PHP

Constructor used in object oriented language for allocate space while creating object. A constructor also helps initialize a variable while declaring.

Every object oriented language built in support for programming. PHP is also a server side script and object oriented language. So should know how can you use constructor is elaborate in following example.


<?php
class Foo
{
    private 
$roll, $name, $city;
    function 
__construct($roll, $name, $city)
    {
        
$this->roll = $roll;

        $this->name = $name;
        
$this->city = $city;
    }
    function 
show()
    {
        echo 
"Roll=" . $this->roll . "<br> Name=" . $this->name . "<br> City=" . $this->city;
    }
  
}
?>
<!DOCTYPE html>
<html >
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">

<title>Girfa : Student Help :: COnstructor</title>
        
</head>

<body>
<?php
$ob 
= new Foo("101", "Sona", "Vns");
$ob->show();

?>    
</body>
</html>


No comments:

Post a Comment