PHP object-oriented reflection function and usage

墨辰丷
Release: 2023-03-27 19:18:01
Original
1442 people have browsed it

This article mainly introduces the object-oriented reflection function and usage of php, and briefly analyzes the concept and specific usage of php5 object-oriented reflection in the form of examples. Friends in need can refer to it

The examples in this article describe PHP object-oriented reflection function and usage. Share it with everyone for your reference, the details are as follows:

Personal understanding of the definition of reflection:

First of all, we have to talk about what reflection is. For a novice, the concept of reflection often gives people a feeling that they don't understand it, and they don't know how to operate it.

Reflection refers to: refers to extending the analysis of PHP programs in the running state of PHP, exporting or extracting detailed information about classes, methods, properties, parameters, etc., and also Include comments. This function of dynamically obtaining information and dynamically calling object methods is called reflection API. Reflection is an API for manipulating meta-models in the object-oriented paradigm. It is very powerful and can help us build complex and scalable applications. (Note: This kind of reverse operation in is only fully available after PHP5)

I will use an example to illustrate it below:

class test{
     private   $A;
     public   $B;
     protected  $C;
     public function test(){
       return "this is a test function";
     }
}
//实例化一个反射类ReflectionClass
$obj=new ReflectionClass('test');
echo $obj."<br>";
//实例化test类,并访问其test方法
$obj2=$obj->newInstance();
echo $obj2->test();
Copy after login

Personal instance return result:

/** * xxx.php * ============================================== * Copy right 2012-2015 * ---------------------------------------------- * This is not a free software, without any authorization is not allowed to use and spread. * ============================================== * @Author:YeXianMing * @Email:LangWaiShiGe@hotmail.com * @Version:zend studio10.6.2 php5.4.38 apache2.2 */ Class [ class test ] { @@ D:\www\MyProjecttest\index5.php 13-21 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [3] { Property [ private $A ] Property [ public $B ] Property [ protected $C ] } - Methods [1] { Method [ public method test ] { @@ D:\www\MyProjecttest\index5.php 18 - 20 } } }
this is a test function
Copy after login

The above is the entire content of this article , I hope it will be helpful to everyone’s study.


Related recommendations:

ThinkPHP Function details: D method

PHPCommon methods to obtain file extensions

About PHPRelevant knowledge points about implementing user login (2 )

The above is the detailed content of PHP object-oriented reflection function and usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!