Re: [RFC] Nullsafe calls
| From: | Andrea Faulds | Date: | Fri, 12 Dec 2014 16:13:27 +0000 |
| Subject: | Re: [RFC] Nullsafe calls | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79602@lists.php.net to get a copy of this message | ||
Hi Thomas,
> On 12 Dec 2014, at 15:42, Thomas Bley <mails@thomasbley.de> wrote:
>
> 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.
This would prevent adding methods for scalars or arrays in future, so I don’t like it.
Thanks.
--
Andrea Faulds
http://ajf.me/