NYCPHP Meetup

NYPHP.org

[nycphp-talk] Question about explicit returns

Brian D. brian at realm3.com
Fri Jan 11 14:11:58 EST 2008


I actually would go with:

<?php
public function checkForSomething($record_id) {
        $yourObj = new SomeObject;

        if (
                $yourObj->loadObjectByRecordId($record_id)
                && is_array($restrictedItems = $yourObj->getUnrestrictedItems()
                && $yourObj->updateUnrestrictedItems(self::ObjectItemStatus)
        ) {
                $this->setRecordStatusId($yourObj->getRecordStatusId());
                $this->setRestrictedItemList($restrictedItems);
                return true;
        }

   return false;
}

Since the && operator is executed in the order you want, extra if()s
are unnecessary.

I agree with Allen, though, that it doesn't matter so much as just
being consistent. Much of it is personal preference or company policy:
basically who decides what is more readable.

- Brian D.

On Jan 11, 2008 1:56 PM, Michael B Allen <ioplex at gmail.com> wrote:
> On 1/11/08, Tod Dailey <christiandailey at gmail.com> wrote:
> > When I'm writing a function that returns a Boolean that indicates that the
> > given process failed or <snip> Verse inline returns, such as "if
> > ($condition !== true) return false;" that builds a list of failure by
> > exception.
>

[snip]


-- 
realm3 web applications [realm3.com]
freelance consulting, application development
(423) 506-0349



More information about the talk mailing list