Martin Scotta

2011/4/28 Johannes Schlüter <johan...@schlueters.de>

> On Thu, 2011-04-28 at 20:04 +0200, Ferenc Kovacs wrote:
> > > Why I need being "saved" from documenting my code properly? Anyway, the
> > > function code defines the behavior, declaration of return type just
> ensures
> > > function would fail in runtime if your code tries to return unexpected
> data
> > > - but how is it helpful? The client of this function doesn't even know
> that
> > > before actually calling it!
> >
> >
> > why did we added ppp if people can define in the documentation(or in the
> > method name :/) that which method is public and which isn't
>
> with ppp i prevent the client code from doing something the
> function/class author didn't expect. With this return hint the function
> auther can force a runtime error when doing something wrong himself.
> (which he has to check one way or the other using tests etc.)
>
>
Return types are meant to ensure that you get what you asked for. This is
really useful when you need to delegate to some component, specially if it's
not of your own, although there are ways to avoid that patterns.

IMHO I would not trust on any return value, as PHP did not ensure anything
about them.
Even more, I do not write code that depend on return values, I prefer to
use input/output parameters,

This is the safer way I've found to achieve the same behavior

function no_return(ReturnStatus $returnStatus) {
   doSomething();

   if ( itWasOk() )
   {
       $returnStatus->setReturnValue( new Foo ); // return type hint
       $returnStatus->success(); // return status
   }
   elseif ( isWasCritical() )
   {
       $returnStatus->setException( new RuntimeException );
       $returnStatus->exception(); // return status
   }
   else
   {
       $returnStatus->fail(); // return status
   }
}

Of course I don't write all functions like this.
This is pattern I use when I need to ensure that the type returned is valid.

johannes
>
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply via email to