NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Handle Multiple Return Types


Joined: 16 Dec 2009
Posts: 4
Reply with quote
Per the phpDoc documentation, @return may contain a set of pipe-delimited return types. The most common use case for this would be error handling when exceptions aren't being used, and code completion in that case is not as useful.

However, we are using code that when an array of ids are passed in, an array of corresponding objects are returned. If a single value is passed in, a single object (not an array of objects) is returned.

Currently, phpEd does not seem to support multiple return type parameters. They work individually, and if a pipe delimiter is used, only the value preceding the pipe is respected by phpEd. This request is for supporting multiple return type functionality with phpDoc comments.

Code:

class Something{
    /** @var string $id */
    public $id;

    public function __construct($id){
        $this->id = $id;
    }

    /**
    * Get instantiated objects of Something
    *
    * @var $id string|array Request one or more ids
    * @return Something|Something[]  Returns a single instance if a single id was passed, else returns an associative array where the key is the id from the array, and the value is a Something object instance.
    public static function get($id){
        if(is_array($id)){
            $return = array();
            foreach($id AS $i){
                $return[$i] = new Something($i);
            }
            return $return;
        }
        return new Something($id);
    }

}
View user's profileFind all posts by duerraSend private message


Joined: 21 Nov 2009
Posts: 38
Reply with quote
Have your tried it the otherway around, eg, Something[]|Something? This works for me using arrays but not sure in your situation.
View user's profileFind all posts by waynesSend private message
Handle Multiple Return Types
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic