Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of php Code reuse Mechanism

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "detailed introduction of php code reuse mechanism". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

When it comes to code reuse in php, we may immediately think of inheritance, but once this single inheritance language has too many subclasses, it will give rise to a series of problems, such as relying on the parent class, too much coupling, and breaking the encapsulation of the class. So is there a better way to achieve code reuse?

Since PHP 5.4.0, PHP has implemented another method of code reuse called traits.

Traits is a code reuse mechanism for single inheritance languages like PHP. In order to reduce the limitation of single inheritance language, Trait gives developers the freedom to reuse method sets in separate classes within different hierarchies. The semantics of Traits and class composition define a way to reduce complexity and avoid the typical problems associated with traditional multi-inheritance and Mixin.

Basic usage

The use of Traits is very simple, as long as you use the use keyword in the class.

Trait A {public function test () {echo 'trait A::test ()';}} class b {use A;} $b=new b (); $b-> test ()

Priority

To put it simply, the Trait priority is higher than the parent class method, but less than the current class method.

Trait A {public function test () {echo 'trait A::test ()';} public function test1 () {echo 'trait A::test1 ()';}} class base {public function test () {echo 'base::test ()';} public function test1 () {echo 'base::test1 ()';}} class b extends base {use A Public function test () {echo 'b::test ()';}} $b=new b (); $b-> test (); / / b::test () $b-> test1 (); / / trait A::test1 ()

Trait conflict problem

When using multiple Trait, there is a conflict if the same method name exists in it. Method name conflicts can be resolved by using insteadof and as

Insteadof can declare the use of a specific method in the same method name.

Trait A {public function test () {echo 'trait A::test ()';}} trait B {public function test () {echo 'trait B::test ()';} class c {use A Magi B {A::test insteadof Bten hand / use insteadof to specify which method B::test as testB is used / / to modify another method name using as, you must use} $c=new c (); $c-> test (); / / trait A::test () $c-> testB (); / / trait B::test () after using insteadof to resolve conflicts.

Method access control

Using the as keyword, we can modify the access rights of the trait method.

Trait A {public function test () {echo 'trait A::test ()';} private function test1 () {echo 'trait A::test1 ()';}} class b {use A {test as protected; test1 as public test2;// can also modify the name}} $b=new b (); $b-> test (); / / Fatal error: Call to protected method b::test () $b-> test2 () / / trait A::test1 ()

Trait nesting use

Trait A {public function test1 () {echo 'test1';}} trait B {public function test2 () {echo' test2';}} trait C {use A Magazine B;} class D {use C;} $d = new D (); $d-> test2 (); / / test2

Variable, property, method definition

Trait can define properties, but properties with the same name cannot be defined in a class

Trait A {public $test1;} class B {use A; public $test; public $test1;//Strict Standards: B and A define the same property ($test1) in the composition of B. }

Trait supports abstract methods, static methods, and static variables cannot be defined directly, but static variables can be referenced by trait methods.

Trait A {public function test1 () {static $a = 0; $echo $a;} abstract public function test2 (); / / definable abstract method} class B {use A; public function test2 () {}} $b = new B (); $b-> test1 () / / 1$ b-> test1 (); / / 2 compare javascript

The use of this trait use is probably similar to call in javascript, where another object is mounted to the current object's execution environment. Of course, javascript is a prototype-based language. The two are not comparable either. Just use it in much the same way, which helps to understand.

Function a () {this.name= "a"; this.getName=function () {console.log (this.name);}} function b () {this.name= "b"; a.call (this);} var b = new b (); b.getName (); / / a

Because the variable environment in javascript is function-based, an is output

This is the end of the introduction to the detailed introduction of php code reuse mechanism. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 230

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report