Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPdoc - defining object properties for an object of stdClass

Tags:

I am trying to figure out if it is possible to use PHPdoc to define the object properties being returned by a function or a object method.

Say I have the following class:

class SomeClass {     public function staffDetails($id){          $object = new stdClass();         $object->type = "person";         $object->name = "dave";         $object->age = "46";                  return $object;     } } 

Now, it is easy enough to define input parameters.

 /**  * Get Staff Member Details  *   * @param   string  $id    staff id number  *   * @return  object  */  class SomeClass {     public function staffDetails($id){         $object = new stdClass();         $object->type = "person";         $object->name = "dave";         $object->age = "46";                  return $object;     } } 

The question is is there a similar thing for defining properties of the output object (of a stdClass) returned by the method in question. So that another programmer does not have to open this class and manually look into the method to see what the return object is returning?

like image 810
someuser Avatar asked Sep 15 '12 07:09

someuser


People also ask

What is an stdClass object?

The stdClass is a generic empty class used to cast the other type values to the object. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. The stdClass is not the base class for objects in PHP.

How do I print a stdClass object?

If you just want to print you can use var_dump() or print_r() . var_dump($obj); print_r($obj); If you want an array of all properties and their values use get_object_vars() .

How do you access the properties of an object in PHP?

The most practical approach is simply to cast the object you are interested in back into an array, which will allow you to access the properties: $a = array('123' => '123', '123foo' => '123foo'); $o = (object)$a; $a = (array)$o; echo $o->{'123'}; // error!


1 Answers

Here it is 4 years later, and there still does not appear to be a way to annotate the properties of a stdClass object as originally described in your question.

Collections had been proposed in PSR-5, but that appears to have been shot down: https://github.com/php-fig/fig-standards/blob/211063eed7f4d9b4514b728d7b1810d9b3379dd1/proposed/phpdoc.md#collections

It seems there are only two options available:

Option 1:

Create a normal class representing your data object and annotate the properties.

class MyData {     /**      * This is the name attribute.      * @var string      */     public $name;      /**      * This is the age attribute.      * @var integer      */     public $age; } 

Option 2:

Create a generic Struct type class as suggested by Gordon and extend it as your data object, using the @property annotation to define what generic values are possible to access with __get and __set.

class Struct {     /**      * Private internal struct attributes      * @var array      */     private $attributes = [];      /**      * Set a value      * @param string $key      * @param mixed $value      */     public function __set($key, $value)     {         $this->attributes[$key] = $value;     }      /**      * Get a value      * @param string $key      * @return mixed      */     public function __get($key)     {         return isset($this->attributes[$key]) ? $this->attributes[$key] : null;     }      /**      * Check if a key is set      * @param string $key      * @return boolean      */     public function __isset($key)     {         return isset($this->attributes[$key]) ? true : false;     } } 

/**  * @property string $name  * @property integer $age  */ class MyData extends Struct {     // Can optionally add data mutators or utility methods here } 
like image 148
Jeremy Harris Avatar answered Nov 16 '22 07:11

Jeremy Harris