What is inheritence ?
In php you specify that a class is a subset of another by using the keyword extends, which tells PHP that the class you are declaring should inherit all the properties and methods from its parent class, and that you are adding functionality or providing some additional specialization to that class.
Php support two types of inheritence.
Single inheritence
Multiplelevel inhritence
Multiple inheritence (doesn’t supprot directly) : it is access by interface.
File name : cat.php
<?php
class Cat
{public $weight;
public $height;
public $furcolor;
public $maxspeed;
public function eat()
{
print "cat eating";
}
public function hunt(prey $objpry)
{
//gkjhg
}
public function purr()
{
print "purrrr...";
}
}
?>
<?php
require_once 'cat.php';
class Lion extends Cat{
public $lionpower;
public function roar()
{
print "Rorrrrrrrrrrrrrrrrrrrrrrrr...";
}
}
?>
Lion_cat.php
<?php
include 'lion.php';
$obj = new Lion();
$obj->weight = 200;
$obj->furcolor = 'brown';
$obj->lionpower = 'max power';
$obj->roar();
$obj->eat();
$obj->purr();
?>
<?php class Foo {
public function printItem($string)
{
echo 'Foo:'.$string.PHP_EOL;
}
public function printPHP()
{
echo 'PHP is great.'.PHP_EOL;
}
}
class Bar extends Foo {
public function printItem($string)
{
echo 'Bar:'.$string.PHP_EOL;
}
}
$foo = new Foo();
$bar = new Bar();
$foo->printItem('baz'); //Output:'Foo:baz'
$foo->printPHP(); //Output:'PHP is great'
$bar->printItem('baz');// Output:'Bar:baz'
$bar->printPHP(); //Output: 'PHPis great'
?>
Constructor and inheritence :
how do you pass data back to the base class’s constructor. You saw a constructor for the person class that took the person’s name.
Classs Person
{
var $name;
function __construct($data)
{
$thsi->name = $data;
}
}
How could you call the constructor from the friend class, which is based on the Person class.
You can do that like this in the friend class’s constructor
parent::__construct() calls the parent class’s (the friend class’s ) constructor.
function __construct($data)
{
parent::__construct($data);
}
And you can pass other data to this constructor as well . Data targeted for the friend class.
<?php
class Person {
var $name;
function __construct($data) {
$this->name=$data; }
function set_name($data) {
$this->name=$data; }
function get_name() {
return $this->name; }
}
class Friend extends Person{
var $message;
function __construct($data, $msg) {
parent::__construct($data); $this->message = $msg;
}
function speak() {
echo $this->message;
}
}
$obj = new Friend("mahtab","danish");
echo "My Name is ", $obj->get_name(), ".<br>";
echo $obj->get_name()," says \"", $obj->speak(), "\".<br>";
?>
Output:- My Name is mahtab. mahtab says "danish".
Calling base class methods
When you have inherited from another class, how do yu call the methods in that base class. Normallly that’s not a problem. Because those methods are available to you to. Because you have inherited them from the base class. But what if the base class has methods that are protected. And you want to make them public by letting any code call them.
<?php
class Person {
var $name;
protected function set_name($data) {
$this->name=$data; }
protected function get_name() {
return $this->name; }
}
class Friend extends Person{
var $message;
function set_message($msg) {
$this->message = $msg; }
function speak() {
echo $this->message; }
function set_name_public($data) {
parent::set_name($data); }
function get_name_public() {
return parent::get_name(); }
}
$obj = new Friend;
$obj->set_name_public("mahtab");
$obj->set_message("Hello from mahtab");
echo "the Name is ", $obj->get_name_public(), ".<br>";
echo $obj->get_name_public()," says \"", $obj->speak(), "\".<br>";
?>
Output :-
the Name is mahtab. mahtab says "Hello from mahtab".
Previous
Next