php object-oriented -- inheritance, polymorphism

Posted by merlinti on Mon, 09 Sep 2019 14:25:46 +0200

The second major feature of object-oriented: inheritance

Inheritance: The common attributes and methods of all subclasses are extracted and placed in the parent class, and then inherited, that is to say, the repetitive code only needs to be written once.

Why use inheritance?

Inheritance allows us to get rid of duplicate code and expand our functionality.

There are constructors in the parent class to implement inheritance:

<?php
 
/**
 * Automobile
 */
class Car
{
    public $wheel; //Car wheels
    function __construct($wheel)
    {
        $this->wheel = $wheel;
    }
 
    public function run()
    {
        echo 'Can run';
    }
 
    public function stop()
    {
        echo 'Brake';
    }
}
 
/**
 * Truck class, inherited automobile class
 */
class Truck extends Car
{
    public $load; //Loading capacity
}
 
/**
 * Bus class, inherit automobile class
 */
class Bus extends Car
{
    public $peples; //Passenger capacity
 
    public function say(){
        echo 'Reporting station';
    }
}
 
 
$t = new Truck(12);//Transfer parameters
printf("Trucks have%d A wheel",$t->wheel);
echo '<br>';
$b = new Bus(6);//Transfer parameters
printf("Buses have%d A wheel",$t->wheel);

Both the construction method of the subclass and the construction method of the parent class are executed:

<?php
 
/**
 * Automobile
 */
class Car
{
    public $wheel; //Car wheels
    function __construct($wheel)
    {
        $this->wheel = $wheel;
    }
 
    public function run()
    {
        echo 'Can run';
    }
 
    public function stop()
    {
        echo 'Brake';
    }
}
 
/**
 * Truck class, inherited automobile class
 */
class Truck extends Car
{
    public $load; //Loading capacity
    function __construct($wheel)
    {
        //Call the construction method of the parent class
        parent::__construct($wheel);
    }
 
}
 
/**
 * Bus class, inherit automobile class
 */
class Bus extends Car
{
    public $peples; //Passenger capacity
    function __construct($wheel)
    {
        //Call the construction method of the parent class
        parent::__construct($wheel);
    }
 
    public function say(){
        echo 'Reporting station';
    }
}
 
 
$t = new Truck(12);//Transfer parameters
printf("Trucks have%d A wheel",$t->wheel);
echo '<br>';
$b = new Bus(6);//Transfer parameters
printf("Buses have%d A wheel",$t->wheel);

In the construction method of the subclass, parent::_construct($wheel) is used; the construction method of the parent class is invoked actively.

Inheritance-protected:Protected

protected-modified members of the parent class are used only within the class and in inherited classes, but not outside the class.

<?php
 
/**
 * Class Controller
 */
class Controller
{
    private $arr = [];
 
    protected function assign($name,$value)
    {
        $this->arr[$name] = $value;
    }
 
    protected function display()
    {
        foreach ($this->arr as $key => $value) {
            echo "{$key}={$value}<br>";
        }
    }
 
}
 
/**
 * Class IndexController
 */
class IndexController extends Controller
{
    public function show()
    {
        $this->assign('no', '1001');
        $this->assign('name', 'zhangsan');
        $this->display();
    }
}
 
$index = new IndexController();
$index->show();

Polymorphism--abstract class

Polymorphism: Different objects show different forms for the same behavior.

The characteristics of abstract methods are as follows:

1. The abstract method has no method body

2. Classes with abstract methods must be abstract classes

The characteristics of abstract classes:

1. Abstract classes cannot be implemented and have no objects of their own.

2. All abstract classes are parent classes

Attention should be paid to inheriting Abstract classes:

After subclasses inherit Abstract classes, they must implement all abstract methods of the parent class.

<?php
 
//abstract class
abstract class Animal
{
    //Abstract method
    abstract protected function eat();
}
 
//Tiger, inherit animals
class Tiger extends Animal
{
    //Specific realization
    public function eat()
    {
        echo 'Tigers gnaw at food to eat';
    }
}
 
//Snake, inherit animals
class Snake extends Animal
{
    //Specific realization
    public function eat()
    {
        echo 'Snakes swallow food directly.';
    }
}
 
$t = new Tiger();
$t->eat();
echo '<br>';
$s = new Snake();
$s->eat();

Polymorphism--Coverage

When overwriting (overwriting) methods of parent classes, what attention should be paid to accessing modifiers:

1. Method Name Consistency

2. Access permissions of subclass methods are higher than those of parent methods.

<?php
 
//Customer category
class Customer
{
    public $name; //Full name
 
    //Purchase of goods
    public function buy()
    {
        echo 'Purchase at the original price';
    }
}
 
//Class vip
class Vip extends Customer
{
    public $vipcard;//vip card
 
    //Purchase of goods
    public function buy()
    {
        echo 'Goods can be bought at a discount.';
    }
}
 
$c = new Customer();
$c->buy();
echo '<br>';
$v = new Vip();
$v->buy()

 

Topics: PHP