These two files demonstrate how inheritance works with constructors and the parent constructor. The first file shows how properties are declared within the constructor, while the second file uses the parent constructor(parent::__construct()) to inherit properties. Both files are available for previewing.
Accessing Properties Without Parent Constructor
class Blogger extends Bohemia
{
protected $_email;
public function __construct($category, $first_name, $email)
{
$this->first_name = $first_name.” and his friend Eugene can be contacted at”;
$this->category = $category;
$this->email = $email;
}
public function get_email()
{
return $this->email;
}
}
$blogger = new Blogger(‘PHP’, ‘Johnny’, ‘nerdos@example.com’);
echo $blogger->get_first_name().’ the email address “‘.$blogger->get_email().'”. They love ‘.$blogger->get_category().’.’;
?>
Accessing Properties With Parent Constructor
class Blogger extends Bohemia
{
protected $_email;
public function __construct($category, $first_name, $email)
{
parent::__construct($category, $first_name);
$this->email = $email;
}
public function get_email()
{
return $this->email;
}
}
$blogger = new Blogger(‘PHP’, ‘Johnny’, ‘nerdos@example.com’);
echo $blogger->get_first_name().’ the email address “‘.$blogger->get_email().'”. They love ‘.$blogger->get_category().’.’;
?>
© 2018 ALL Rights Reserved | Sitemap
Vancouver | North Vancouver | Burnaby | Richmond | Coquitlam | Port Coquitlam | Surrey | Langley | Maple Ridge | Mission | Abbotsford | Kamloops | Kelowna | Victoria | Squamish | Whistler | Pemberton