|
Re: inheritance, fluent interfaces, and code completion [message #87325 is a reply to message #87247] |
Sat, 20 December 2008 08:11 |
Eclipse User |
|
|
|
Originally posted by: michael.zend.com
Is setRole() declared as abstract method in your Article class ?
"Geraint Howell" <G.S.J.Howell@Swansea.ac.uk> wrote in message
news:gig7dl$ugu$1@build.eclipse.org...
> Hi.
>
> I have an abstract base class called Article. It has a mutator like this:
>
> /**
> * Set the Title of the Article
> *
> * @param string $title
> * @return Article
> */
> public function setTitle($title = null)
> {
> if (! is_string($title))
> throw new Exception('Invalid title');
> $this->title = $title;
> return $this;
> }
>
> I have a concrete sub class called News Article. It has a mutator like
> this:
>
> /**
> * Sets the Role that is permitted to view this article
> *
> * @param string $role
> * @return NewsArticle
> */
> public function setRole($role = null)
> {
> if (! is_string($role))
> throw new Exception(sprintf("Invalid role: '%s'", $role));
> $this->role = $role;
> return $this;
> }
>
> This fluent interface allows me to write code like this:
>
> $article = new NewsArticle();
> $article
> ->setRole('public')
> ->setTitle('Foo Bar');
>
> PDT gives me nice code completion for this.
>
> However, code completion breaks when I use methods from the abstract class
> (even though the code works just fine):
>
> $article
> ->setTitle('Foo Bar')
> ->setRole('public'); // no code completion here
>
> I guess this is because setTitle has @return Article in the doc block.
>
> I could change it to @return Article|NewsArticle, but this feels wrong,
> since the base class shouldn't know anything about classes that derive
> from
> it.
>
> Is there a better way to do it?
>
>
|
|
|
|
Powered by
FUDForum. Page generated in 0.03990 seconds