【问题标题】:OO PHP - retrieving all attributes from a classOO PHP - 从类中检索所有属性
【发布时间】:2012-02-07 15:56:08
【问题描述】:

是否有从类中检索所有属性的函数/方法?比如下面我班级的所有属性:

class plantOfTheMonth {

   //Declare which centre(s) are being used
    private $centre = "";

    //Declare the attributes of the current Plant Of The Month
    private $name = "";
    private $latinName = "";
    private $image = "";
    private $imageAlt = "";
    private $imageLink = "";
    private $strapLine = "";
    private $description = "";
    private $color = "";
    private $centres = "";

    //Declare variables for error handling
    private $issue = "";
    private $issueCode = "";

}

【问题讨论】:

    标签: php oop class attributes


    【解决方案1】:

    【讨论】:

    • get_object_vars() 只会获取可访问的属性,因此当从对象外部调用时,只会返回公共属性。
    • 感谢 Crozin,我在上面的答案中的链接中使用了这个函数,就像在课堂上这样解决这个问题:public function test() {var_dump(get_object_vars($this)); }
    【解决方案2】:

    foreach 也可以完成这项工作,it iterates trough the visible class properties

    【讨论】:

      猜你喜欢
      • 2011-08-14
      • 2021-12-30
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2017-09-01
      • 1970-01-01
      相关资源
      最近更新 更多