"PHP Classes and objects" anonymous class

Source: Internet
Author: User

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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.