Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93578 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 79787 invoked from network); 26 May 2016 17:09:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 May 2016 17:09:28 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.82 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.82 mx101.easyname.com Received: from [77.244.243.82] ([77.244.243.82:35052] helo=mx205.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CC/EB-17600-5CD27475 for ; Thu, 26 May 2016 13:09:26 -0400 Received: from cable-81-173-133-15.netcologne.de ([81.173.133.15] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1b5ymg-00068i-VQ; Thu, 26 May 2016 17:09:23 +0000 Reply-To: internals@lists.php.net References: <20160525215208.034FC1A801B3@dd1730.kasserver.com> <5fd54aa0-4fdf-c1e7-eae8-765aa89c8498@fleshgrinder.com> <871d57cd-cf8d-626c-8bfc-4c23482fb38d@fleshgrinder.com> To: Tom Worster , php-internals Message-ID: Date: Thu, 26 May 2016 19:09:10 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="qlEq1DckJndL7lP7pEfXIH8NsPs70O7Ql" X-ACL-Warn: X-DNSBL-BARRACUDACENTRAL Subject: Re: [PHP-DEV] [RFC][Vote] Typed Properties From: php@fleshgrinder.com (Fleshgrinder) --qlEq1DckJndL7lP7pEfXIH8NsPs70O7Ql Content-Type: multipart/mixed; boundary="ippdGhTwtP46UL4uVXn9kUEuLHbSIROgq" From: Fleshgrinder Reply-To: internals@lists.php.net To: Tom Worster , php-internals Message-ID: Subject: Re: [PHP-DEV] [RFC][Vote] Typed Properties References: <20160525215208.034FC1A801B3@dd1730.kasserver.com> <5fd54aa0-4fdf-c1e7-eae8-765aa89c8498@fleshgrinder.com> <871d57cd-cf8d-626c-8bfc-4c23482fb38d@fleshgrinder.com> In-Reply-To: --ippdGhTwtP46UL4uVXn9kUEuLHbSIROgq Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 5/26/2016 7:00 PM, Tom Worster wrote: > On 5/26/16, 12:48 PM, "Fleshgrinder" wrote: >> This would be a valid approach too, yes. I personally would be against= >> it because I do not want to initialize all my properties. >> >> class A { >> >> private int $x; >> >> public function getX() { >> if (empty($this->x)) { >> $this->x =3D 42; >> } >> return $this->x; >> } >> >> } >> >> This would not yield an E_NOTICE because both isset() and empty() neve= r >> do. This allows the attentive programmers to keep up there coding >> practices without the necessity to assign meaningless values everywher= e. >> >> class A { >> >> /** -1 is invalid */ >> public int $x =3D -1; >> >> /** 'INVALID' is invalid but empty string is allowed */ >> public string $s =3D 'INVALID'; >> >> /** Null byte is invalid but anything else is valid */ >> public string $welcome_to_the_c_world =3D '\0'; >> >> } >=20 > If you want that kind of thing, you can do it the old PHP way like this= >=20 > class A { > private ?int $x =3D null; > ... >=20 Doing that defeats the whole purpose of typed properties in my opinion. I want to be sure that nobody can assign null to this property and only allow it to be null until I initialize it myself. class A { private int $x; private function getX() { if (empty($this->x)) { $this->x =3D 42; } return $this->x; } public function doSomething() { // lots of code ... $_ =3D $__ * $this->x; // lots of code ... } } This would emit an E_NOTICE (and possibly end up in an error due to null) and the developer of the doSomething() method will notice that she should call getX() instead. This would not be the case with private ?int $x. :( --=20 Richard "Fleshgrinder" Fussenegger --ippdGhTwtP46UL4uVXn9kUEuLHbSIROgq-- --qlEq1DckJndL7lP7pEfXIH8NsPs70O7Ql Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJXRy26AAoJEOKkKcqFPVVrghYQAK9xArIlmF42xA5/GdnsvVCo 0LnwbqZe1pfqtL0PKU9NC2IuXn+lXPzKNtwG7gOxPyrziF/IEtTbL3qmtx0k9YyX 5nCkQlE81uJVO8Ud7AjygeGvppRFvDxjwdhSrBNfzVIqcKdtH1nv2VMG8pvLHBB/ jEqb+22LtKUN7LJ6p192qXPANrqyUcvB/SX1HVvRUH5QGMrOwkETrLUMBk+V4biK 9BOaLNAUocFXHWt1yw/YkXoyBboiXbWpDCGerwj2vu2l2FJDbiPKNUKQuZpG5Y7f 3Q7XRF4Ni4PUSJ2zh1yhGVX3mEK9H4X0hgUQ898OsBAnkgz5SS3T3BDHx1fHx3wP r08Shke+IHe1K1AqiXGAotvi3lhIhZ5UoVqx/1oA+N1ftjZPOcxUoOO/5327u4eB b+CaaPdym9/lHXQTG/Zn3QtvCH0RUcEb9x7mM4N/PJwkBBslKmc9M4gRZHaPl7Cd W/o9/IDI3+NUogEQ5+o9tCHTZKE7ZC/EQhwtq2h832miFwZZMLPi1v+eRMXR4Coj OxBJP2REkNljudDYhUp4674teUnWS8Cpkwb0UWBDvsdsytIzOAKVNhxC08ox+OL4 Su1UawPWSdwwayeP4ydLEVx9HLmvq2LwLfOLrMOVfH2rko50Cd6KeedW4+TE7EVi Sq549jkLztesBRKk3LyP =QgIT -----END PGP SIGNATURE----- --qlEq1DckJndL7lP7pEfXIH8NsPs70O7Ql--