The reflection mechanism of PHP

Source: Internet
Author: User

The most classic use of object-oriented is reflection, which, in the Java language, can be decoupled using reflection for dependency injection.

In PHP, too, there is such a powerful place where we use reflection to get an instance of an object.

First we'll write a class:

1 classtestclass{2     Private $number;3     Private $name;4 5      Public function__construct () {6         $this- Number=123;7         $this->name= "Test";8     }9 Ten      Public functionPrintstu_info () { One         Echo"The student number is {$this->number},and name is {$this->name} "; A     } -}

In PHP, you can use the Reflectionclass class to get the details of this class. An instance of this class can be obtained by calling its Newinstance method.

Use this to get an instance of the class, and the effect of new will be the same.

1 prod_class=new reflectionclass (' TestClass '); 2 $instance= newinstance_class, $prod(); 3 $instance->printstu_info ();

The output of this program is the student number are 123,and name is test

In general, we can use the following methods:

1 $test=new  TestClass (); 2 $test->printstu_info ();

The reflection mechanism of PHP

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.