Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79600 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 63288 invoked from network); 12 Dec 2014 15:42:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Dec 2014 15:42:40 -0000 Authentication-Results: pb1.pair.com header.from=mails@thomasbley.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=mails@thomasbley.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain thomasbley.de from 85.13.137.24 cause and error) X-PHP-List-Original-Sender: mails@thomasbley.de X-Host-Fingerprint: 85.13.137.24 dd15934.kasserver.com Received: from [85.13.137.24] ([85.13.137.24:51859] helo=dd15934.kasserver.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 58/00-62606-CEC0B845 for ; Fri, 12 Dec 2014 10:42:38 -0500 Received: from dd15934.kasserver.com (dd0802.kasserver.com [85.13.143.1]) by dd15934.kasserver.com (Postfix) with ESMTPSA id 145FA261D8D; Fri, 12 Dec 2014 16:42:33 +0100 (CET) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-SenderIP: 92.222.45.59 User-Agent: ALL-INKL Webmail 2.11 To: jwatzman@fb.com Cc: internals@lists.php.net Message-ID: <20141212154233.145FA261D8D@dd15934.kasserver.com> Date: Fri, 12 Dec 2014 16:42:33 +0100 (CET) Subject: Re: [PHP-DEV] [RFC] Nullsafe calls From: mails@thomasbley.de ("Thomas Bley") Hello Josh, 1) I think it would be better to have ObjectSafe Calls, e.g. class User { ... public function delete() { // delete ... } } class Users { public function find($id) { if ($this->db->query(...)) return new User($id); return 'not found'; } } $users = new Users(); $result = $users->find(42)?->delete(); // $result = 'not found'; That way we can use scalars as return for further processing. Definition: Calling $obj?->foo(..) behaves identically to $obj->foo(..) if $obj is an object. If $obj is not an $object, then it returns $obj. 2) Another approach would to implement this in user land: class Failure { public function __construct($msg) { $this->msg = $msg; } public function __call($name, $args) { return $this; } } class User { ... public function delete() { // delete ... } } class Users { public function find($id) { if ($this->db->query(...)) return new User($id); return new Failure('not found'); } } $users = new Users(); $result = $users->find(42)->delete(); // do sth... if ($result instanceof Failure) { echo $result->msg; // not found } Regards Thomas Josh Watzman wrote on 10.12.2014 00:07: > Hey internals! A useful feature that Hack picked up in the last few months are > "nullsafe calls", a way of propagating failure forward in a series of chained > method calls to the end of the whole computation, getting rid of a lot of the > boilerplate in the middle. I think the feature would be a good one for PHP as > well, so I'm submitting this RFC to add it -- you can see the RFC itself for a > full discussion of the motivation for the feature, as well as the feature > itself: > > https://wiki.php.net/rfc/nullsafe_calls > > Josh Watzman > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >