Home  >  Article  >  Backend Development  >  Is PHP multiple inheritance or single inheritance?

Is PHP multiple inheritance or single inheritance?

王林
王林Original
2021-10-18 17:51:233057browse

php is single inheritance. PHP does not support multiple inheritance, but PHP can implement multiple inheritance by using interface or trait, such as [interface test1 {public function connect();}interface test2...].

Is PHP multiple inheritance or single inheritance?

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

Is php single inheritance or multiple inheritance? Maybe you have been confused by this problem for a long time. First of all, PHP is single inheritance and does not support multiple inheritance. The characteristics of object-oriented are encapsulation, inheritance, and polymorphism. Inheritance here refers to the inheritance relationship between classes, which can be achieved using the keyword extends. Only one class can be inherited here.

So, can PHP implement multiple inheritance? The answer is yes.

php can implement multiple inheritance in two ways. One is to use interface implementation. Another way is to use traits.

The first one: Regarding the use of interface implementation, the principle is that one class can implement multiple interfaces, and we can define multiple interface classes, as follows:

interface test1 {
	public function connect();
}
interface test2 {
	public function contact();
}

One class implements multiple interfaces :

class MyClass implements test1,test2 {
	public function connect() {
		echo "test1";
	}
	public function contact() {
		echo "test2";
	}
}

Second: Use trait implementation. In fact, traits are not considered multiple inheritance. They should be called functions similar to multiple inheritance.

What is trait?

Answer: It looks like both a class and an interface, but it is actually neither. Trait can be regarded as a partial implementation of the class and can be mixed into one or more existing PHP classes. It has two functions: indicating What a class can do; provide a modular implementation. Trait is a code reuse technology that provides a flexible code reuse mechanism for PHP's single inheritance restriction.

//基本类
  class basicTest{
    public function test(){
      echo "hello,world\n";
    }
  }

  //traitOne
  trait traitOne{
    public function test(){
      echo "this is trait one";
    }
    public function testOne(){
      echo "one";
    }
  }

  //traitTwo
  trait traitTwo{
    // public function test(){
      // echo "this is trait two!";
    // }

    public function testTwo(){
      echo "Two";
    }
  }

  //继承基本类,并use trait
  class myCode extends basicTest{
    use traitOne,traitTwo;
    public function test(){
      echo "hehaha!!";
    }
  }

  $obj = new myCode();
  $obj->testTwo();

Note:

Priority: own method>trait method>inherited method (this is what it looks like.)
If we open the comments in the above code, it will An error is reported because the methods in the two traits have the same name.

How to solve the problem of duplicate method names in traits?

//使用demo1和demo2的方法,但并不是导入命名空间
//demo1和demo2种都有hello方法
    use Demo1,Demo2{
        //将Demo1的hello方法替换Demo2的hello方法
        Demo1::hello insteadof Demo2;
        //给Demo2的hello方法起别名
        Demo2::hello as Demo2Hello;
    }
//下方调用的时候
return $this->hello(); // 使用demo1的方法
return $this->Demo2Hello();  //使用demo2的方法

Recommended learning: php training

The above is the detailed content of Is PHP multiple inheritance or single inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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