Example Analysis of Three Methods of Obtaining Object Attributes in PHP

  • 2021-11-13 06:48:13
  • OfStack

This paper illustrates three methods of obtaining object attributes by PHP. Share it for your reference, as follows:

Looking at the source code of yii today, we found that in yii\ base\ Model attribute() The method is to get the public non-static property of the object through reflection. Remember that all the codes I saw before used get_object_vars() This function is retrieved. Looking at the php document yesterday, we found that we can also traverse object properties with foreach. So write an example to practice.


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 ($this as $k => $v) {
      echo $k, ' => ', $v, PHP_EOL;
    }
  }
  public function getPropertyMethodThree() {
    echo "[reflection]", PHP_EOL;
    $class = new ReflectionClass($this);
    foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
      if (!$property->isStatic()) {
        echo $property->getName(), ' => ', $property->getValue($this), PHP_EOL;
      }
    }
  }
}

Test code 1


$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;
  }
}

Output:

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

It can be seen that when the three methods are used outside the class, the result is one. get_object_vars() And foreach get the public non-static attributes of the object, while the public non-static attributes need to be filtered manually through reflection.

Test code 2


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

Output

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

It can be seen that, get_object_vars() private, protected, public instance properties can be obtained when foreach and foreach are used inside the class. Needless to say, reflection can be obtained by manual filtering.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: