In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
I believe many inexperienced people don't know what to do about how to use anonymous classes in PHP. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The usage of PHP anonymous class
After PHP7, the feature of anonymous classes was added to PHP. Anonymous classes and anonymous methods make PHP a more modern language and make our code development more and more convenient. Let's first look at the simple use of anonymous classes.
/ / define directly
$objA = new class
{
Public function getName ()
{
Echo "Ihumm objA"
}
}
$objA- > getName ()
/ / return in the method
Function testA ()
{
Return new class
{
Public function getName ()
{
Echo "I'm testA's obj"
}
}
}
$objB = testA ()
$objB- > getName ()
/ / as a parameter
Function testB ($testBobj)
{
Echo $testBobj- > getName ()
}
TestB (new class {
Public function getName ()
{
Echo "I'm testB's obj"
}
});
Three ways to use anonymous classes are given at one time. Anonymous classes can be defined directly to variables, returned using return in a method, or passed as parameters to the inside of the method. In fact, an anonymous class is like a class that is not defined in advance and is instantiated directly at the time of definition.
/ / inheritance, interface, access control, etc.
Class A
{
Public $propA ='A'
Public function getProp ()
{
Echo $this- > propA
}
}
Trait B
{
Public function getName ()
{
Echo 'trait B'
}
}
Interface C
{
Public function show ()
}
$p4 = 'b4'
$objC = new class ($p4) extends An implements C
{
Use B
Private $prop1 = 'b1'
Protected $prop2 = 'b2'
Public $prop3 = 'b3'
Public function _ _ construct ($prop4)
{
Echo $prop4
}
Public function getProp ()
{
Parent::getProp ()
Echo $this- > prop1,'= =', $this- > prop2,'= =', $this- > prop3,'= =', $this- > propA
$this- > getName ()
$this- > show ()
}
Public function show ()
{
Echo 'show'
}
}
$objC- > getProp ()
Like ordinary classes, anonymous classes can inherit other classes, implement interfaces, and of course include a variety of access control capabilities. In other words, anonymous classes are no different from normal classes in terms of usage. But if you use get_class () to get the class name, it will be the class name automatically generated by the system. The same anonymous class returns the same name, of course.
/ / the name of the anonymous class is given by the engine
Var_dump (get_class ($objC))
/ / declare the same anonymous class, and the objects created are all instances of this class
Var_dump (get_class (testA ()) = get_class (testA ()
What about static members in anonymous classes? Of course, like ordinary classes, static members belong to the class, not the instance.
/ / static variable
Function testD ()
{
Return new class {
Public static $name
}
}
$objD1 = testD ()
$objD1::$name = 'objD1'
$objD2 = testD ()
$objD2::$name = 'objD2'
Echo $objD1::$name
After reading the above, have you mastered how to use anonymous classes in PHP? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.