美文网首页
PHP获取对象属性方法

PHP获取对象属性方法

作者: 手扶拖拉机_6e4d | 来源:发表于2019-01-15 14:10 被阅读0次

    今天查看源码,发现yii\base\Model中的attribute()方法是通过反射获取对象的public non-static属性。记得以前看到的代码都是用get_object_vars()这个函数获取的(返回由对象属性组成的关联数组),昨天查看php文档,发现还可以用foreach遍历对象属性。于是写个例子练习下:

    class TestClass{
        private $a;
        protected $b;
        public $c;
        public $d;
        public static $e;
        private function funcA() {
        }
        protected function funcB() {
        }
        public function funcC() {
        }
    
        public function getPropertyMethodOne(){
            echo "[get_object_vars]",PHP_EOL;
            $vars = get_object_vars($this);
            //获取对象属性列表数组,再遍历数组
            foreach ($vars as $k => $v){
                echo $k, ' => ', $v, PHP_EOL;
            }
        }
        public function getPropertyMethodTwo(){
            echo "[foreach object]",PHP_EOL;
            //foreach遍历对象属性
            foreach ($this as $k=>$v){
                echo $k, ' => ', $v, PHP_EOL;
            }
        }
        public function getPropertyMethodThree(){
            echo "[reflection]", PHP_EOL;
            $class = new \ReflectionClass($this);
            //反射获取类的public属性
            foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property){
                if(!$property->isStatic()){
                    echo $property->getName(), ' => ' , $property->getValue($this), PHP_EOL;
                }
            }
        }
    }
    

    测试demo1:

    $obj = new TestClass();
            echo "[get_object_vars]",PHP_EOL;
            $vars = get_object_vars($obj);
            foreach($vars as $k=>$v ){
                echo $k, ' => ', $v, PHP_EOL;
            }
            echo "[foreach object]", PHP_EOL;
            foreach ($obj as $k=>$v){
                echo $k ,' => ', $v, PHP_EOL;
            }
            echo "[reflection]", PHP_EOL;
            $class = new \ReflectionClass($obj);
            foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property){
                if(!$property->isStatic()){
                    echo $property->getName(), ' => ' , $property->getValue($obj), PHP_EOL;
                }
            }
    

    输出:

    [get_object_vars]
    c => 
    d => 
    [foreach object]
    c => 
    d => 
    [reflection]
    c => 
    d =>  
    

    可见,当这三种方法用在类的外部时,结果是一样的。get_object_vars()和foreach是获取到的是对象的public non-static属性,而通过反射的话则需要手动筛选出public non-static属性。
    测试demo2:

            $obj = new TestClass();
            $obj->getPropertyMethodOne();
            $obj->getPropertyMethodTwo();
            $obj->getPropertyMethodThree();
    

    输出:

    [get_object_vars]
    a => 
    b => 
    c => 
    d => 
    [foreach object]
    a => 
    b => 
    c => 
    d => 
    [reflection]
    c => 
    d => 
    

    可见,get_object_vars()和foreach用在类内部时,private、protected、public的实例属性都能获取到。反射就不用说了,通过手动筛选,各种类型的属性都能获取到。
    类的知识点补充:

    namespace App\Model;
    class TestClass {
        private $a = '初始值a' ;
        protected $b;
        public $c;
        public $d;
        public static $e;
    
        public function getPropertyMethodThree(){
            $this->a = 'wudy.yu';
            $class = new \ReflectionClass($this);
            echo $class->getName() , PHP_EOL;  //App\Model\TestClass
            echo $class->name,PHP_EOL;  //App\Model\TestClass
            var_dump($class->getDefaultProperties()). PHP_EOL;  //获取所有属性的初始默认值(后面赋值的不算)
            echo $class->getEndLine(),PHP_EOL; //获取类的最后行的行数
        }
    }
    

    输出:

    App\Model\TestClass
    App\Model\TestClass
    array(5) {
      ["e"]=>
      NULL
      ["a"]=>
      string(10) "初始值a"
      ["b"]=>
      NULL
      ["c"]=>
      NULL
      ["d"]=>
      NULL
    }
    50
    

    相关文章

      网友评论

          本文标题:PHP获取对象属性方法

          本文链接:https://www.haomeiwen.com/subject/ogqydqtx.html