PHP - 打印对象的所有属性

sta*_*ker 58 php debugging

我在php页面中有一个未知对象.

如何打印/回显它,以便我可以看到它具有哪些属性/值?

功能怎么样?有没有办法知道对象有什么功能?

Pet*_*tai 85

<?php var_dump(obj) ?>
Run Code Online (Sandbox Code Playgroud)

要么

<?php print_r(obj) ?>
Run Code Online (Sandbox Code Playgroud)

这些与您用于数组的内容相同.

这些将显示使用PHP 5的对象的受保护和私有属性.静态类成员将不会根据手册显示.

如果您想知道成员方法,可以使用get_class_methods():

$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name) 
{
    echo "$method_name<br/>";
}
Run Code Online (Sandbox Code Playgroud)

相关内容:

get_object_vars()

get_class_vars()

get_class() < - 表示实例的名称


fel*_*ins 11

由于没有人没有提供OO方法,这里就像它会做的那样.

class Person {
    public $name = 'Alex Super Tramp';

    public $age = 100;

    private $property = 'property';
}


$r = new ReflectionClass(new Person);
print_r($r->getProperties());

//Outputs
Array
(
    [0] => ReflectionProperty Object
        (
            [name] => name
            [class] => Person
        )

    [1] => ReflectionProperty Object
        (
            [name] => age
            [class] => Person
        )

    [2] => ReflectionProperty Object
        (
            [name] => property
            [class] => Person
        )

)
Run Code Online (Sandbox Code Playgroud)

使用反射的优点是您可以通过属性的可见性进行过滤,如下所示:

print_r($r->getProperties(ReflectionProperty::IS_PRIVATE));
Run Code Online (Sandbox Code Playgroud)

由于Person :: $属性是私有的,因此在按IS_PRIVATE过滤时会返回:

//Outputs
Array
(
    [0] => ReflectionProperty Object
        (
            [name] => property
            [class] => Person
        )

)
Run Code Online (Sandbox Code Playgroud)

阅读文档!

  • 经典代码示例就在那里.给予好评! (2认同)

med*_*iev 5

var_dump($obj); 
Run Code Online (Sandbox Code Playgroud)

如果您想了解更多信息,可以使用ReflectionClass:

http://www.phpro.org/manual/language.oop5.reflection.html


Dev*_*vWL 5

要获取更多信息,请使用此自定义 TO($someObject) 函数:

我写了这个简单的函数,它不仅显示给定对象的方法,还显示它的属性、封装和一些其他有用的信息,如发布说明(如果给出的话)。

function TO($object){ //Test Object
                if(!is_object($object)){
                    throw new Exception("This is not a Object"); 
                    return;
                }
                if(class_exists(get_class($object), true)) echo "<pre>CLASS NAME = ".get_class($object);
                $reflection = new ReflectionClass(get_class($object));
                echo "<br />";
                echo $reflection->getDocComment();

                echo "<br />";

                $metody = $reflection->getMethods();
                foreach($metody as $key => $value){
                    echo "<br />". $value;
                }

                echo "<br />";

                $vars = $reflection->getProperties();
                foreach($vars as $key => $value){
                    echo "<br />". $value;
                }
                echo "</pre>";
            }
Run Code Online (Sandbox Code Playgroud)

为了向您展示它是如何工作的,我现在将创建一些随机示例类。让我们创建名为 Person 的类,并在类声明上方放置一些发行说明:

        /**
         * DocNotes -  This is description of this class if given else it will display false
         */
        class Person{
            private $name;
            private $dob;
            private $height;
            private $weight;
            private static $num;

            function __construct($dbo, $height, $weight, $name) {
                $this->dob = $dbo;
                $this->height = (integer)$height;
                $this->weight = (integer)$weight;
                $this->name = $name;
                self::$num++;

            }
            public function eat($var="", $sar=""){
                echo $var;
            }
            public function potrzeba($var =""){
                return $var;
            }
        }
Run Code Online (Sandbox Code Playgroud)

现在让我们创建一个 Person 的实例并用我们的函数包装它。

    $Wictor = new Person("27.04.1987", 170, 70, "Wictor");
    TO($Wictor);
Run Code Online (Sandbox Code Playgroud)

这将输出关于类名、参数和方法的信息,包括封装信息和参数数量、每个方法的参数名称、方法位置和存在的代码行。请参阅下面的输出:

CLASS NAME = Person
/**
             * DocNotes -  This is description of this class if given else it will display false
             */

Method [  public method __construct ] {
  @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 75 - 82

  - Parameters [4] {
    Parameter #0 [  $dbo ]
    Parameter #1 [  $height ]
    Parameter #2 [  $weight ]
    Parameter #3 [  $name ]
  }
}

Method [  public method eat ] {
  @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 83 - 85

  - Parameters [2] {
    Parameter #0 [  $var = '' ]
    Parameter #1 [  $sar = '' ]
  }
}

Method [  public method potrzeba ] {
  @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 86 - 88

  - Parameters [1] {
    Parameter #0 [  $var = '' ]
  }
}


Property [  private $name ]

Property [  private $dob ]

Property [  private $height ]

Property [  private $weight ]

Property [ private static $num ]
Run Code Online (Sandbox Code Playgroud)