获取PHP类的所以属性和所有方法,可通过反射机制

PHP开发 | 2013-01-15 08:55:07 | 阅读 16074 次 | 评论(0)
<?php

class Class1{
    public $var1 = 'var1';
    public $var2 = 'var2';
    private $var3 = 'var3';

    public function method1(){}
    public function method2(){}
    private function method3(){}
}

print_r(get_class_methods(Class1));
print_r(get_class_vars(Class1));


//用反射类可以获得私有属性和私有方法
$ref = new ReflectionClass(Class1);

print_r($ref->getMethods());
print_r($ref->getProperties());

输出

Array
(
    [0] => method1
    [1] => method2
)
Array
(
    [var1] => var1
    [var2] => var2
)
Array
(
    [0] => ReflectionMethod Object
        (
            [name] => method1
            [class] => Class1
        )

    [1] => ReflectionMethod Object
        (
            [name] => method2
            [class] => Class1
        )

    [2] => ReflectionMethod Object
        (
            [name] => method3
            [class] => Class1
        )

)
Array
(
    [0] => ReflectionProperty Object
        (
            [name] => var1
            [class] => Class1
        )

    [1] => ReflectionProperty Object
        (
            [name] => var2
            [class] => Class1
        )

    [2] => ReflectionProperty Object
        (
            [name] => var3
            [class] => Class1
        )

)

文章评论,共0条
游客请输入验证码
浏览1857186次
文章归档
最新评论
  • 静夜思:它下面的,直到ob_get_clean() 为止
  • 静夜思:9年了,当年解决这个问题的思路记忆犹新😊