Find property area inside class

class ParentClass
{
    public function list()
    {
        foreach ($this as $property => $value)
        {
            if (is_public($this->$property))
                echo 'public: ';
            else if (is_protected($this->$property))
                echo 'protected: ';
            echo "$property => $value" . PHP_EOL;
        }
    }
}

class ChildClass extends ParentClass
{
    protected $Size = 4;
    protected $Type = 4;
    public $SubT = 1;
    public $UVal = NULL;
}

$CC = new ChildClass;
$CC->list();
+3
source share
1 answer

Using ReflectionPropertyit is possible. You can create a helper function if you want to make it less detailed:

<?php
function P($obj, $name)
{
  return new ReflectionProperty($obj, $name);
}

class Foo
{
  public $a;

  public function __construct()
  {
    foreach (array_keys(get_object_vars($this)) as $name)
    {
      if (P($this, $name)->isPublic())
      {
        echo "Public\n";
      }
    }
  }
}

new Foo();

?>
+5
source

Source: https://habr.com/ru/post/1767690/


All Articles