Home > Backend Development > PHP Tutorial > PHP object-oriented inheritance

PHP object-oriented inheritance

不言
Release: 2023-03-29 22:26:02
Original
1577 people have browsed it

This article mainly introduces object-oriented inheritance in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Inheritance

Definition

The subclass inherits all the parent class’s-public and protected properties and methods.

Use the keyword extendsInheritance. One class integrates another and cannot inherit more than one.

Related professional terms

子类、父类、基类、超类、派生类
Copy after login

Subclass, derived class——> Class inherited from the base class;

Parent class, base class, super class refers to——> ; Inherited class.

Example

<?php

class Father
{
    public $a = &#39;public&#39;;
    protected $b = &#39;protected&#39;;
    private $c = &#39;private&#39;;

    public function func1()
    {
        echo &#39;This is public function&#39;,&#39;<br/>&#39;;
    }

    protected function func2()
    {
        echo &#39;This is protected function&#39;,&#39;<br/>&#39;;
    }

    private function func3()
    {
        echo &#39;This is private function&#39;,&#39;<br/>&#39;;
    }
}

// 继承父类
class Child extends Father
{
    // 子类中调用继承来的方法
    public function test()
    {
        $this -> func1();
        $this -> func2();
        //$this -> func3();  私有方法不可以调用
    }
}

// 实例化子类
$son = new Child();


// 尝试调用属性
echo $son -> a,&#39;<br/>&#39;;
//echo $son->b;   不能直接输出b
//echo $son -> c; 继承都没继承,当然不能输出


// 尝试调用方法
//$son -> func1();
//$son -> func2();   不能在类外调用func2
//$son -> func3();   继承都没继承,当然不能调用

// 间接调用继承自父类的普通方法
$son -> test();
Copy after login

Rewrite

Definition:

1) Inherit the method of the parent class, and the definition in the subclass is the same as that of the parent class Method with the same name in class , and has the same number of parameters ;

2) When the subclass overrides the method in the parent class, PHP will not call the method in the parent class Overridden method;

3) Whether to call the method of the parent class depends on the subclass.

4) When implementing method coverage, the access modifier can be different , but the access scope of the subclass must be greater than or equal to the parent class Access scope .

Usage:

parent::    访问父类中被重写的属性和方法
Copy after login

Example

<?php

class Father
{
    public function shuo($num)
    {
        echo &#39;a a o o&#39;;
    }
}


class Son extends Father
{
    public function shuo($num)
    {
        // 调用父类中被重写的方法
        // parent::shuo($num);

        echo &#39;Hello World&#39;;
    }

    public function test($num)
    {
        parent::shuo($num);
    }
}

// 实例化子类
$son1 = new Son();
$son1 -> shuo(1);   // Hello World
$son1 -> test(1);   // a a o o
Copy after login

final keyword

If the method in the parent class is declared final, then Subclasses cannot override this method.

If a class is declared final, it cannot be inherited.

Example

<?php

class Father
{
    final public function test()
    {
        echo &#39;hello&#39;;
    }
}

class Son extends Father
{
    public function test()
    {
        echo &#39;你好&#39;;
    }
}

// 实例化子类
$son1 = new Son();

$son1 -> test();  // 报错,函数不能被重写
Copy after login

Related recommendations:

php object-oriented overloading

php object-oriented Encapsulation

The above is the detailed content of PHP object-oriented inheritance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template