How to use Traits to implement multiple inheritance in PHP? (code example)

青灯夜游
Release: 2023-04-05 11:34:01
Original
3115 people have browsed it

PHP does not support multiple inheritance, but you can use interfaces or Trait attributes to use multiple inheritance. In the previous article [How does PHP use interfaces to implement multiple inheritance? ] introduced the method of using interfaces to implement multiple inheritance. The following article will take you to understand the Trait feature and introduce the method of using the Trait feature to implement multiple inheritance in PHP. I hope it will be helpful to everyone.

How to use Traits to implement multiple inheritance in PHP? (code example)

What is Trait?

Trait is a new concept introduced in PHP 5.4, which removes the limitation of multiple inheritance in PHP. Prior to PHP 5.4, PHP supported single inheritance and multiple interfaces, but Traits will remove the limitation of not having multiple inheritance. [Video tutorial recommendation: PHP tutorial]

Traits is a mechanism for reusing code in single inheritance languages ​​(such as PHP). Its main concept is code reusability. Traits seem to be very useful in languages ​​that only support single inheritance such as PHP. Therefore, the main reason to use traits is to gain the benefits of multiple inheritance, or code reusability.

Use Trait attributes to implement multiple inheritance

1. Use classes and Trait attributes

Basic Sentence pattern:

class child_class_name extends parent_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
Copy after login

Code example:

<?php 
header("content-type:text/html;charset=utf-8");   
// Hello类
class Hello  { 
  public function sayhello() { 
     echo "<br>Hello"; 
  } 
} 
  
// Phpzww 特性
trait  Phpzww{ 
  public function sayfor() { 
     echo " php中文网"; 
  } 
} 
  
class Sample extends Hello { 
   use Phpzww; 
   public function demo() { 
      echo "<br><br>学习编程!"; 
  }  
} 
  
$test = new Sample(); 
$test->sayhello(); 
$test->sayfor(); 
$test->demo(); 
?>
Copy after login

Output:

How to use Traits to implement multiple inheritance in PHP? (code example)

Code description:

in the above code , "traits" are used with the parent class. There is a class called "Hello" which contains the function sayhello() and a trait called "Phpzww" which contains the function demo() and there is a subclass called "Sample" and we are creating this An object of a class named "test" and use it to call all functions in the class and trait attributes.

2. Use multiple Trait features

Basic sentence structure:

class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
Copy after login

Code example:

<?php 
header("content-type:text/html;charset=utf-8");   
// 特性:Hello 
trait Hello{ 
  public function sayhello() { 
     echo "<br>Hello"; 
  } 
} 
  
// 特性:Phpzww
trait  Phpzww{ 
  public function sayfor() { 
     echo " php中文网!"; 
  } 
} 
  
class Sample { 
  use Hello; 
  use Phpzww; 
  public function Helloforphp() { 
      echo "<br><br>欢迎学习PHP!"; 
   }  
} 
  
$test = new Sample(); 
$test->sayhello(); 
$test->sayfor(); 
$test->Helloforphp(); 
?>
Copy after login

Output:

How to use Traits to implement multiple inheritance in PHP? (code example)

In the above code, multiple Trait attributes are used to implement multiple inheritance. There are two features named "Hello" and "Phpzww", which contain the sayhello() and sayfor() functions respectively; there is also a "sample" subclass, which contains the Helloforphp() function. We create an object of the class named "test" and use it to call all the functions of the traits attribute.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to use Traits to implement multiple inheritance in PHP? (code example). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!