简体   繁体   中英

What is the best way to look inside a PHP class instance (object) to see all of its available public properties and methods?

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.

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.

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() . "\n";
}

参见php手册中的get_class_methods

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM