【问题标题】:How to print all properties of an object如何打印对象的所有属性
【发布时间】:2011-03-03 08:21:51
【问题描述】:

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

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

函数呢?有没有办法知道一个对象有什么功能?

【问题讨论】:

  • (@Brad Lowry 想和你分享一些东西。我已经逐字复制了他的文字,但没有对其内容做出任何贡献):@ 之间有 两个区别987654321@ 和var_dump()var_dump() 可以接受多个 $expression 参数(没什么大不了的)。但是,print_r() 有一个可选参数$return,默认为 FALSE,但可以设置为 TRUE,这使得函数“返回”输出而不是仅仅表达它。如果您想收集 print_r() 结果然后在输出底部的开发人员“块”中表达它,这将非常有用。
  • @varocarbas, "但可以设置为 TRUE"
  • @Coisox 老实说,我什至不记得我写这篇文章的确切原因(我猜是没有足够声誉的人试图通过发布一个新答案来分享这些想法,我将其删除为我的审核职责),但很明显你应该感谢布拉德洛瑞而不是我。即使不记得那个确切的时刻,也忽略了我对真实作者的明确提及,我可以告诉你,我肯定没有写过那篇文章的任何部分。
  • 第二个可选参数的使用令人难以置信!对于网店中的 $product 对象,我现在可以通过error_log(...,0) 在调试日志中使用print_r($products,True) 而不是get_object_vars($product)。还有一个额外获取对象变量的键值,在我的例子中,它们被组织为一个关联数组。我想知道,为什么 print_r($product) 返回 1 作为结果。非常感谢布拉德·洛瑞!
  • 使用 print_r 进行调试时的提示:始终将它与指定为 true 的第二个参数一起使用以禁止错误。例如。 error_log("print_r(\$product) = ".print_r($product),0); 在我的情况下导致连接器脚本出错,而 error_log("print_r(\$product,true) = ".print_r($product,true),0); 很好。 (并且还给出了所需的输出:-)

标签: php debugging


【解决方案1】:
<?php
    echo "<textarea name='mydata'>\n";
    echo htmlspecialchars($data)."\n";
    echo "</textarea>";
?>

【讨论】:

  • 欢迎来到 Stack Overflow,感谢您提供答案。您能否编辑您的答案以包括对您的代码的解释?这将帮助未来的读者更好地理解正在发生的事情,尤其是那些不熟悉该语言并努力理解这些概念的社区成员。在有公认的答案的情况下,这一点尤其重要。在什么情况下,您的方法会优于已接受的答案或任何其他答案?
【解决方案2】:

要获取更多信息,请使用此自定义 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>";
            }

为了向您展示它是如何工作的,我现在将创建一些随机示例类。让我们创建一个名为 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;
            }
        }

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

    $Wictor = new Person("27.04.1987", 170, 70, "Wictor");
    TO($Wictor);

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

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 ]

【讨论】:

【解决方案3】:

尝试使用Pretty Dump,它对我很有用

【讨论】:

    【解决方案4】:

    由于没有人没有提供 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
            )
    
    )
    

    使用反射的好处是可以通过属性的可见性进行过滤,像这样:

    print_r($r->getProperties(ReflectionProperty::IS_PRIVATE));
    

    由于 Person::$property 是私有的,它在通过 IS_PRIVATE 过滤时返回:

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

    阅读文档!

    【讨论】:

      【解决方案5】:

      了解对象属性 var_dump(object) 是最好的方法。它将显示与其关联的所有公共、私有和受保护的属性,而无需知道类名。

      但是对于方法,你需要知道类名,否则我认为很难获取对象的所有关联方法。

      【讨论】:

        【解决方案6】:
        <?php var_dump(obj) ?>
        

        <?php print_r(obj) ?>
        

        这些也是你用于数组的东西。

        这些将显示 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/>";
        }
        

        相关内容:

        get_object_vars()

        get_class_vars()

        get_class()

        【讨论】:

        • 函数呢?有没有办法知道一个对象有什么功能?
        • 我在上面的get_class_methods()中添加了。
        • 这里显示的是类数据,而不是具体的对象数据。
        • 另外,您可以添加 html
           以获得更干净的输出。
        【解决方案7】:
        var_dump($obj); 
        

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-28
          • 2010-10-25
          • 2019-01-31
          • 1970-01-01
          • 2013-07-11
          • 1970-01-01
          • 2021-10-09
          • 2020-01-02
          相关资源
          最近更新 更多