Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91709 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32920 invoked from network); 16 Mar 2016 19:21:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Mar 2016 19:21:14 -0000 Authentication-Results: pb1.pair.com header.from=derokorian@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=derokorian@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.174 as permitted sender) X-PHP-List-Original-Sender: derokorian@gmail.com X-Host-Fingerprint: 209.85.214.174 mail-ob0-f174.google.com Received: from [209.85.214.174] ([209.85.214.174:34980] helo=mail-ob0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 70/EC-48430-922B9E65 for ; Wed, 16 Mar 2016 14:21:14 -0500 Received: by mail-ob0-f174.google.com with SMTP id fp4so61372401obb.2 for ; Wed, 16 Mar 2016 12:21:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=5xA6mu9AE3axpkOQuEMuxP9/rVSe5L0O7297qGyDt1E=; b=O43emjpikC1l44cRbSwL/1A2v9eiuqi8tjuUesbiDqsYDqDFvSyxRoLTB1Urtcr6Vq 8eP7QY7fsyfoQ46KMpLug3os1I+xlifDzBkAbSDA1o6NHREMAO6sJYr3oQnyLTtJ3KYi bHU1tGRBBRbTZT2NBdwFslA7NL0aNccjG86h75oO3te/sAfFwcxTvcNZe9pmpW+Ce+wY +37o8Zm3yEyzb7jCWO8KnwZMz/spzwemEv5/1+QIQJ76Axt9N1+Iv+YalX56HIQKmXQh Sc2pnJzXVDXsXWgmCRnCEidU3hewCKuh5dD6yP9cZ5xeulqlzf7ox0LbAXmJKooY2JkP NQDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=5xA6mu9AE3axpkOQuEMuxP9/rVSe5L0O7297qGyDt1E=; b=dQdSNJ53MyPUlFZh6yOfAfO8yWsbatFbOe3Aqid+n89NvhaDGgjskTiu6ZdkG3GtMS 5R05LUOG29B+JhxHCPvBYKSDODm8xdzA3BJ401EZDHj7XFI8WARPwov6iZz9wbUxJhIH /T7XrOLGGjyNtKlSbpHadLEq2Oj54doe2qCfqgjiJoGeM7etnKusEi0VQMBaTqrd7hCy LrwzoFiQ0JQFmQohgss8vPgIs1dKOs4isMnphz9HSQCEMpQI9IDbh5OvZ74ydJ32kvvR qmJTz2D2qI/m28LPhRs98/0B3An0PDL583Dl4et9nsrN8+eNePpH8GV4nCfLKkj+gV05 O/XA== X-Gm-Message-State: AD7BkJJFd1VSIqbng7XUWBdRso+WA0Xnf0/g7RRKXjZ1Mx2kqUxB1BFvV+MhYdTP1VZODDDwHOl4+2q+FbYE/g== MIME-Version: 1.0 X-Received: by 10.182.79.167 with SMTP id k7mr3771273obx.43.1458156070714; Wed, 16 Mar 2016 12:21:10 -0700 (PDT) Received: by 10.157.4.200 with HTTP; Wed, 16 Mar 2016 12:21:10 -0700 (PDT) In-Reply-To: <56E9AAF1.7070200@garfieldtech.com> References: <56E9AAF1.7070200@garfieldtech.com> Date: Wed, 16 Mar 2016 13:21:10 -0600 Message-ID: To: Larry Garfield Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=089e0139fc989f42b5052e2f6a6e Subject: Re: [PHP-DEV] [RFC Discussion] Typed Properties From: derokorian@gmail.com (Ryan Pallas) --089e0139fc989f42b5052e2f6a6e Content-Type: text/plain; charset=UTF-8 On Wed, Mar 16, 2016 at 12:50 PM, Larry Garfield wrote: > On 3/16/16 11:36 AM, Phil Sturgeon wrote: > >> >> 2. This whole temporary nullability situation, where unset properties >> will error on attempted usage if not set. Should they instead error >> after the constructor has been called if they are still not holding a >> value? >> > > I fall back to a statement I made in a blog post a while back: > > http://www.garfieldtech.com/blog/empty-return-values > > " But consider what you could do instead that would not force me to throw > is_null() around my code, because throwing is_null() around my code makes > me sad. NULL should be the return value of last resort, because it means > nothing. (Literally.)" > > Allowing default-null on properties means that as someone using that > property, I have two options: > > 1) Throw a lot of is_null() calls around my code. > 2) Assume that whoever initialized the code provided a value by the time > initialization is done and skip those extra checks. > > Insert that old adage about what happens when you assume. > > End-of-constructor checks seem like a good approach; they have to be > uninitialized at some point when new is called initially, but they should > be guaranteed set as soon as possible. End of the constructor is "as soon > as possible", and I think reasonably static-analysis-catchable. (Meaning > my IDE can yell at me appropriately.) > > That removes is_null() calls from the rest of my codebase, which is a good > thing. > > I'm not sure I like the "after contructor" because that's not necessarily when the object is finished being initialized. Consider this: abstract class Contract { private function __construct() {} public static function fromArray(array $aProps) : Contract { $s = new static(); foreach ($aProps as $k => $v) { if (property_exists(static::class, $k)) { $s->$k = $v; } } return $s; } } class User extends Contract { public int $id; public string $name; } $user = User::fromArray(['id' => 123, 'name' => 'derokorian']); In this example, the constructor finishes but the properties still are not initialized at time of finishing. This is a pretty common pattern as well. --089e0139fc989f42b5052e2f6a6e--