PHP实现多继承的两种方式

多继承是面向对象程序语言的一个特性,支持子类继承多个父类。



PHP通过Interfaces或者Traits实现多继承。

Traits是特殊的类。PHP中虽然类、case 类、traits都不能继承多个父类,但是可以同时使用多个traits.

一,traits

1.1, traits跟类一起用。
traits使用规则:
class child_class_name extends parent_class_name{
    use traits_name;
    ......
    child_class functions
}
比如:
<?php
/**
 * Created by PhpStorm.
 * User: hc
 * Date: 2019/3/5
 * Time: 9:24
 */
class Geeks{
    public function sayhello(){
        echo "hello";
    }
}

trait forGeeks{
    public function sayfor(){
        echo "\nGeeks";
    }
}

class Sample extends Geeks{
    use forGeeks;
    public function geeksforgeeks(){
        echo "<br>GeeksforGeeks";
    }
}

$test = new Sample();
$test->sayhello();
$test->sayfor();
$test->geeksforgeeks();

运行结果:
hello Geeks
GeeksforGeeks

1.2,使用多个traits
类文件中use语句可以使用多个用逗号分隔的traits。
使用规则:
class child_class_name {
    use trait_name;
    ......
    ......
    child_class functions
}

比如:
<?php
/**
 * Created by PhpStorm.
 * User: hc
 * Date: 2019/3/5
 * Time: 9:24
 */
trait Geeks{
    public function sayhello(){
        echo "hello";
    }
}

trait forGeeks{
    public function sayfor(){
        echo "\nGeeks";
    }
}

class Sample{
    use Geeks,forGeeks;
    public function geeksforgeeks(){
        echo "<br>GeeksforGeeks";
    }
}

$test = new Sample();
$test->sayhello();
$test->sayfor();
$test->geeksforgeeks();

运行结果:
hello Geeks
GeeksforGeeks

二, interface
2.1 interface 和 类
使用规则:
child_class name extends parent_class_name implements interface_name1,......
比如
<?php
/**
 * Created by PhpStorm.
 * User: hc
 * Date: 2019/3/5
 * Time: 9:40
 */
class A {
    public function insideA(){
        echo "im in class a";
    }
}
interface B{
    public function insideB();
}

class Multi extends A implements B{
    function foo()
    {
        // TODO: Implement foo() method.
    }

    function baz(Baz $baz)
    {
        // TODO: Implement baz() method.
    }

    function insideB()
    {
        // TODO: Implement insideB() method.
        echo "im in interface";
    }
    public function insideMultiple(){
        echo "\nI am in inherited class";
    }
}

$test = new Multi();
$test->insideA();
$test->insideB();
$test->insideMultiple();

运行结果:
im in class a
im in interface
I am in inherited class

2.2 使用多个interface
class child_class_name implements interface_name1,interface_name2,...
比如:
<?php
/**
 * Created by PhpStorm.
 * User: hc
 * Date: 2019/3/5
 * Time: 9:40
 */
interface A {
    public function insideA();
}
interface B{
    public function insideB();
}

class Multi implements A, B{
    function foo()
    {
        // TODO: Implement foo() method.
    }

    function baz(Baz $baz)
    {
        // TODO: Implement baz() method.
    }

    function insideB()
    {
        // TODO: Implement insideB() method.
        echo "<br/>im in interface";
    }

    function insideA()
    {
        // TODO: Implement insideA() method.
        echo "I am inside a";
    }

    public function insideMultiple(){
        echo "<br/>I am in inherited class";
    }
}

$test = new Multi();
$test->insideA();
$test->insideB();

$test->insideMultiple();


运行结果:
I am inside a
im in interface

I am in inherited class


原文英文,地址:https://www.geeksforgeeks.org/multiple-inheritance-in-php/

评论0条