首页 > 后端开发 > php教程 > PHP ReflectionClass

PHP ReflectionClass

WBOY
发布: 2016-08-08 09:31:30
原创
1139 人浏览过

<span> 1</span> <span>php
</span><span> 2</span> <span>/*</span><span>*
</span><span> 3</span> <span> * @desc test reflectionclass
</span><span> 4</span> <span> * @author songweiqing
</span><span> 5</span> <span> * @create_time 2015-01-7
</span><span> 6</span> <span> *
</span><span> 7</span>  <span>*/</span>
<span> 8</span>  <span>class</span><span> Test{
</span><span> 9</span>     <span>public</span> <span>$attr1</span> = 'attr1'<span>;
</span><span>10</span>     <span>protected</span> <span>$attr2</span> = 'attr2'<span>;
</span><span>11</span>     <span>private</span> <span>$attr3</span> = 'attr3'<span>;
</span><span>12</span>     <span>const</span> ATTR4 = 'I AM THE ATTRIBUTE 4'<span>;
</span><span>13</span>     <span>public</span> <span>static</span> <span>$attr5</span> = 'attr5'<span>;
</span><span>14</span>     
<span>15</span>     <span>public</span> <span>function</span><span> __construct(){
</span><span>16</span>         
<span>17</span>         self::<span>$attr5</span> = 'I love you baby'<span>;
</span><span>18</span> <span>    }
</span><span>19</span>     <span>public</span> <span>function</span><span> getAttr1(){
</span><span>20</span>         
<span>21</span>         <span>echo</span> <span>$this</span>-><span>attr1;
</span><span>22</span> <span>    }
</span><span>23</span>     <span>//</span><span>获取属性2</span>
<span>24</span>     <span>protected</span> <span>function</span><span> getAttr2(){
</span><span>25</span>         
<span>26</span>         <span>echo</span> <span>$this</span>-><span>attr2;
</span><span>27</span> <span>    } 
</span><span>28</span>     <span>/*</span><span>*
</span><span>29</span> <span>     * @desc 获取属性3
</span><span>30</span> <span>     * @return string 
</span><span>31</span>      <span>*/</span>
<span>32</span>     <span>private</span> <span>function</span><span> getAttr3(){
</span><span>33</span>         <span>echo</span> <span>$this</span>-><span>attr3;
</span><span>34</span> <span>    }
</span><span>35</span>     
<span>36</span>     <span>public</span> <span>static</span> <span>function</span><span> getAttr5(){
</span><span>37</span>         <span>echo</span> self::<span>$attr5</span><span>;
</span><span>38</span> <span>    }
</span><span>39</span> <span>}
</span><span>40</span> 
<span>41</span> <span>$reflection</span> = <span>new</span> ReflectionClass('Test'<span>);
</span><span>42</span> <span>//</span><span>var_dump($reflection->getName());//获取类名getName();
</span><span>43</span> <span>//var_dump($reflection->getConstant("ATTR4"));//获取指定的常量名
</span><span>44</span> <span>//var_dump($reflection->getConstants());//获取一组常量名
</span><span>45</span> <span>//var_dump($reflection->getConstructor());//获取构造函数,没有构造函数返回null
</span><span>46</span> <span>//var_dump($reflection->getDefaultProperties());//获取默认属性,常量属性不包括
</span><span>47</span> <span>//var_dump($reflection->getDocComment());//获取针对该类的注释,对于类中法中的注释,忽略,没有返回false
</span><span>48</span> <span>//var_dump($reflection->getEndLine());//获取类中最后一行行数
</span><span>49</span> <span>//var_dump($reflection->getFileName());//获取定义类的类名
</span><span>50</span> <span>//var_dump($reflection->getMethods());//获取所有类中的方法
</span><span>51</span> <span>//var_dump($reflection->getProperties());//获取所有属性,不包含常量属性
</span><span>52</span> <span>//$instance = $reflection->newInstanceArgs();//实例化反射的该类
</span><span>53</span> <span>//$instance = $reflection->newInstance('Test');实例化指定的类</span>
登录后复制
View Code

以上就介绍了PHP ReflectionClass,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板