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