Constructor is a built in function of an object.Constructors are allow us to initialize an object's properties.
Example:
<?php
class pencilbox {
var$name;
function __construct($name)
{
$this->name = $name;
}
function set_name($name)
{
$this->name = $name;
}
function get_name()
{
return $this->name;
}
} ?>
Explanation:
Example:
<?php
class pencilbox {
var$name;
function __construct($name)
{
$this->name = $name;
}
function set_name($name)
{
$this->name = $name;
}
function get_name()
{
return $this->name;
}
} ?>
Explanation:
- When we are instantiating a class (Creating an object),a property will be passed to the class.
- Here $name is the property.