This article to share the content is about the "PHP Class and Object" Anonymous class, has a certain reference value, the need for friends can refer to
Anonymous class
PHP 7 begins to support anonymous classes.
Action: Create a one-time simple Object
You can pass arguments to the constructor of an anonymous class, you can extend (extend) Other classes, implement interfaces (implement interface), and use trait like any other normal class:
<?phpclass SomeClass {}interface Someinterface {}trait sometrait {}var_dump (new Class) extends SomeClass Implements Someinterface { private $num; Public function __construct ($num) { $this->num = $num; } Use sometrait;}); *outputs:object (class@anonymous) #1 (1) { ["Command line code0x104c5b612": "Class@anonymous":p rivate]=> Int (10)}*/
After an anonymous class is nested into a normal class, it cannot access the private (private), protected (protected) methods, or properties of the external class (Outer Class).
In order to access the external class (Outer Class) protected property or method, the anonymous class can extend (extend) the partial.
In order to use the private property of the outer class (Outer Class), it must be passed in through the constructor:
<?phpclass outer{ Private $prop = 1; Protected $prop 2 = 2; protected function func1 () { return 3; } Public Function Func2 () { return new class ($this->prop) extends Outer { private $prop 3; Public function __construct ($prop) { $this->prop3 = $prop; } Public Function func3 () { return $this->prop2 + $this->prop3 + $this->func1 ();}} ;} } Echo (New Outer)->func2 ()->func3 (); 6
Declares the same anonymous class, and the object that is created is an instance of this class.
The name of the anonymous class is given by the engine, as shown in the following example. Because of the implementation details, you should not rely on this class name.
<?phpecho Get_class (new Class {});//class@anonymousd:\phpstudy2018\phptutorial\www\index.php00500020