Re: Method check - Can someone create a RFC for it?
| From: | Johannes Schlüter | Date: | Wed, 20 Mar 2013 19:05:58 +0000 |
| Subject: | Re: Method check - Can someone create a RFC for it? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-66728@lists.php.net to get a copy of this message | ||
Carlos Rodrigues <carlos.vini@gmail.com> wrote:
>So instead of writing
>if ($obj->image) {
>echo $obj->image->getUrl();
>}
>
>He wrote:
>echo $obj->image->getUrl();
Make the property private and add a getter which can throw an exception if no image is available ...
class C {
private $image;
public function hasImage() { return (bool)$this->image; }
public function getImage() { if (!$this->image) { throw new NoImageException(); } return
$this->image; }
}
echo $obj->geImage()->getUrl(); // Exception which can be caught.
No nee dto change the language and add a new cosntruct (which your developer will forget, if he
forgets the if already ....)
johannes