Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80738 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 86383 invoked from network); 18 Jan 2015 12:33:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Jan 2015 12:33:34 -0000 X-Host-Fingerprint: 80.177.120.119 marston-home.demon.co.uk Received: from [80.177.120.119] ([80.177.120.119:28715] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AA/4B-18613-818ABB45 for ; Sun, 18 Jan 2015 07:33:28 -0500 Message-ID: To: internals@lists.php.net References: <0DD30A0D-E7CA-4150-83E0-8FD46635279C@ajf.me> <8761c6280g.fsf@margaine.com> <54B91D16.70901@gmail.com> <78.22.47555.7C24AB45@pb1.pair.com> <1421519637.40188.1.camel@proposaltech.com> <54BABA93.9070809@gmail.com> In-Reply-To: Date: Sun, 18 Jan 2015 12:33:25 -0000 Lines: 6 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="utf-8"; reply-type=original Content-Transfer-Encoding: 8bit X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 16.4.3528.331 X-MimeOLE: Produced By Microsoft MimeOLE V16.4.3528.331 X-Posted-By: 80.177.120.119 Subject: Re: [PHP-DEV] [RFC] Remove PHP 4 Constructors From: TonyMarston@hotmail.com ("Tony Marston") "Andrea Faulds" wrote in message news:D554C8B8-0BFB-44F7-B23E-8BFC12AE2930@ajf.me... > >Hey Rowan, > >> On 17 Jan 2015, at 19:40, Rowan Collins wrote: >> >> On 17/01/2015 18:33, Todd Ruth wrote: <> >> I don't think using __construct over named-method for constructors really >> has anything to do with "OOP fundamentalism"; it was a design change to >> make certain things simpler (like parent::__construct), and more >> consistent (all reserved magic methods begin with __, so any method not >> beginning with that is safe to use however you like). > >To add on to what you said, there’s also a quite important benefit that >__construct is a lot more obvious in what it does. > >Looking at the following code: > >class Foo { > public $foo, > $bar, > $qux; > public function foobar() { > // ... > } > public function bar() { > // ... > } > public function foo() { > // ... > } > public function baz() { > // ... > } > public function qux() { > // ... > } >} > >It’s not easy to spot the constructor at a glance, and it’s very easy to >miss. > >Compare that to the following: > >class Foo { > public $foo, > $bar, > $qux; > public function foobar() { > // ... > } > public function bar() { > // ... > } > public function __construct() { > // ... > } > public function baz() { > // ... > } > public function qux() { > // ... > } >} > >Far more obvious. If a developer cannot read valid code then it is a developer problem. It is not up to the language to dictate style - it provides the functions and features while style and readability are the sole responsibility of the individual developer. > This actually tripped me up on more than one occasion when updating tests > broken by the removal of PHP 4 constructors in php-src. Perhaps this issue can be solved by the RFC on Default Constructors? See https://wiki.php.net/rfc/default_ctor > Sure, the constructor should probably be the first method, but *even if it > is* it’s still nowhere near as obvious in PHP 4 style. > >Similarly, what does the following do? > > $this->foo(); > >It looks like a normal method call, and it is in a sense. But if you’re in >Bar and inheriting from Foo, that’s a call to the parent class’s >constructor! Perhaps there should be a new rule which says that invoking a constructor with anything other than "new" or "parent::__contruct()" should be illegal, in which case this situation would generate an error. >The following is much more obvious: > > parent::__construct(); > >I think it’s pretty clear why we changed to PHP5-style constructors. They’re >just a lot more recognisable. :) >-- >Andrea Faulds >http://ajf.me/ > > > -- Tony Marston