Search code examples
phpdoctrinesettergetterdocblocks

PHP mixed in getters and setters


Im using Php with doctrine and have a question regarding its setters/getters

Accordning to php.net mixed is "mixed indicates that a parameter may accept multiple (but not necessarily all) types..." but why shall i use it when my $id is an int and will always be ? When should i use the "mixed" in getters and setters such as this ?

/**
 * @param mixed $id
 * @return $this
 */
public function setId($id)
{
    $this->id = $id;
    return $this;
}

/**
 * @return mixed
 */
public function getId()
{
    return $this->id;
}

Solution

  • You should used a mix argument and/or return type when the value type may different. For example if the same function may return an array for one call and an int for another.