In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the use of trait capabilities in PHP". In daily operation, I believe many people have doubts about the use of trait capabilities in PHP. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "the use of trait capabilities in PHP". Next, please follow the editor to study!
The use of trait capability in PHP
I believe you are no stranger to trait, as early as 5.4, trait has already appeared in the new features of PHP. Of course, trait itself means feature, but the main capability of this feature is for code reuse.
As we all know, PHP is a modern object-oriented language. In order to solve the confusion of C++ multiple inheritance, most languages are in the form of single inheritance and multiple interfaces, but this will also make some reusable code have to be implemented through composition, if combinations are to be used, it is inevitable to instantiate classes or use static methods, virtually increasing the memory footprint. In order to solve this problem, PHP officially launched the trait capability. You can think of it as a variant of combinatorial ability.
Trait A
{
Public $a ='A'
Public function testA ()
{
Echo 'This is'. $this- > a
}
}
Class classA
{
Use A
}
Class classB
{
Use A
Public function _ construct ()
{
$this- > a ='B'
}
}
$a = new classA ()
B = new classB ()
$a-> testA ()
$b-> testA ()
From the above code, we can see that trait can be applied to any class, and can define variables, very convenient. The most important thing trait needs to pay attention to is the overload priority of methods with the same name.
Trait B {
Function test () {
Echo 'This is trait Bond'
}
}
Trait C {
Function test () {
Echo 'This is trait Cruise'
}
}
Class testB {
Use B, C
Function test () {
Echo 'This is class TestBalls'
}
}
B = new testB ()
$b-> test (); / / This is class testB!
/ / class testC {
/ / use B, C
/ /}
/ / $c = new testC ()
/ / $c-> test (); / / Fatal error: Trait method test has not been applied, because there are collisions with other trait methods on testC
Here, the test () method is overloaded in our class, and what is output here is the method in the class. If you comment out the test () method in the testB class, an error will be reported. Because the program can't tell which trait you want to use the test () method. We can use insteadof to specify which trait is called by the method to be used.
Class testE {
Use B, C {
B::test insteadOf C
C::test as testC
}
}
$e = new testE ()
$e-> test (); / / This is trait B!
$e-> testC (); / / This is trait C!
Of course, in real development, it is better to standardize the method name as much as possible, and do not have this kind of repetition. In addition, what if the subclass references trait and the parent class defines the same method? Of course, the method inherited by the parent class is still called. The priority of trait is lower than that of normal class inheritance.
Trait D {
Function test () {
Echo 'This is trait dating'
}
}
Class parentD {
Function test () {
Echo 'This is class parentD'
}
}
Class testD extends parentD {
Use D
}
$d = new testD ()
$d-> test (); / / This is trait D!
Finally, abstract methods can also be defined in trait. This abstract method is the method that must be implemented by the class that references the trait, and is consistent with the effect of the abstract method in the abstract class.
Trait F {
Function test () {
Echo 'This is trait favored'
}
Abstract function show ()
}
Class testF {
Use F
Function show () {
Echo 'This is class testFlowers'
}
}
$f = new testF ()
$f-> test ()
$f-> show ()
Trait is really a very flexible feature of PHP. Of course, the more flexible something is, the more we need to understand some of its rules of use, so as to avoid some unforeseen mistakes.
Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/201912/source/trait%E8%83%BD%E5%8A%9B%E5%9C%A8PHP%E4%B8%AD%E7%9A%84%E4%BD%BF%E7%94%A8.php
Reference document: https://www.php.net/manual/zh/language.oop5.traits.php
At this point, the study on "the use of trait capabilities in PHP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.