dongnachuang6635
dongnachuang6635
2011-06-27 20:20
浏览 40
已采纳

查看PHP类实例(对象)以查看其所有可用公共属性和方法的最佳方法是什么?

Sorry, newbie question here but anyways,

I'm trying to use the Google Data API to work with some Google Spreadsheets and I'm trying to use var_dump to look at the structure of the objects I'm receiving from its API calls. I tried using var_dump but it's not giving me what I expect. Most of the properties that it shows me show up as protected like this:

...

["_entryClassName:protected"]
...

and I've tried looking at examples of how the objects properties are being accessed and for the properties that I can actually access with the property access operator (->) I don't even see them in the var_dump output.

So, I'm really confused and I was wondering what is the best way for me to know what are the public properties and methods of my class instance and how many of them are there?

Thanks for any help in advance.

图片转代码服务由CSDN问答提供 功能建议

对不起,新手问题,但无论如何,

我正在尝试 使用Google Data API处理一些Google Spreadsheets,我正在尝试使用var_dump查看我从其API调用中收到的对象的结构。 我尝试使用var_dump,但它没有给我我期望的东西。 它显示的大部分属性都显示为受保护,如下所示:

  ... 
 
 [“_ entryClassName:protected”] 
 ... 
    
 
 

我已经尝试查看如何访问对象属性的示例以及我可以使用属性访问运算符实际访问的属性( - >) 我甚至没有在var_dump输出中看到它们。

所以,我真的很困惑,我想知道什么是最好的方式让我知道什么是公共属性和方法 我的班级实例及其中有多少个?

感谢您提前提供任何帮助。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duan02468
    duan02468 2011-06-27 20:30
    已采纳

    I think you want PHP's ReflectionClass which returns information about a class definition at runtime.

    The getMethods function for example accepts parameters to determine whether it should return information about private, protected, public, static methods etc. Although as it says on php.net,

    This function is currently not documented; only its argument list is available.

    I'm not sure how full the rest of the ReflectionClass documentation is, but this makes me think you may want to get ready for a bit of hacking around to achieve exactly what you want.

    点赞 评论
  • duai4512
    duai4512 2011-06-27 20:28

    I would suggest using an IDE with a debugger for this work.

    However, if you want to do it the hard way, you can use reflection, and especially the ReflectionClass which has a number of useful methods:

    http://www.php.net/manual/en/class.reflectionclass.php

    Example:

    $c = new ReflectionClass( get_class($myObject) );
    $properties = $c->getProperties(
       ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED
       );
    foreach ($properties as $property) {
        echo $property->getName() . "
    ";
    }
    
    点赞 评论
  • dongliao1949
    dongliao1949 2011-06-27 20:29

    You can find the official API documentation within the API documentation of the Zend Framework (Because its part of it): http://framework.zend.com/apidoc/core/ (in package Zend_Gdata)

    As a sidenote: The ZF only implements accessor methods (get*() and set*()) instead of public properties.

    点赞 评论
  • doonbfez815298
    doonbfez815298 2011-06-27 20:29

    See get_class_methods in the php manual.

    点赞 评论
  • douzhenggui8171
    douzhenggui8171 2011-06-27 20:37

    You can use:

    1. get_class_methods() and get_object_vars() functions or
    2. ReflectionClass http://www.php.net/manual/en/class.reflectionclass.php

    See example with the first method:

    <?php
    
    class Test {
        public $public_property       = 'public_property';
        protected $protected_property = 'protected_property';
        private $private_property     = 'private_property';
    
        public function public_method() {}
        protected function protected_method() {}
        private function private_method() {}
    }
    
    $instance = new Test();
    
    // Show public methods
    print_r(get_class_methods($instance));
    // Show public properties
    print_r(get_object_vars($instance));
    
    点赞 评论

相关推荐