Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88502 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57445 invoked from network); 25 Sep 2015 21:42:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Sep 2015 21:42:10 -0000 Authentication-Results: pb1.pair.com header.from=codekestrel@googlemail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=codekestrel@googlemail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 209.85.212.175 as permitted sender) X-PHP-List-Original-Sender: codekestrel@googlemail.com X-Host-Fingerprint: 209.85.212.175 mail-wi0-f175.google.com Received: from [209.85.212.175] ([209.85.212.175:34561] helo=mail-wi0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D1/07-31991-1BFB5065 for ; Fri, 25 Sep 2015 17:42:10 -0400 Received: by wicfx3 with SMTP id fx3so38183149wic.1 for ; Fri, 25 Sep 2015 14:42:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=uAjxJwHc4MswBD30inv3WObdaBT+Fck2jQ3Je4d3Qd0=; b=tnh9qGPIbgKxAeL8sC/g2z3Zxk0kn14j5pXVnSkZtuwDFal2p/qeF+yhN0wwEOUHUQ MpTPqXMZ90ebOtF0ZBWgQfUM8lMJfLuFWs5hOcC62qioh8L0YUtm5M/534+SJTetqBBG yWPXRl/I5YNJA+VcCztw3rd5B2r1Bn2p3ArxHoPeCbqHuAU8DIqs5uXYauwKQ1hg/Em3 PQdd12W4H12j8PPo+oY9p+6dg+xxH7iH7UXbMSrHF8i4P883YYlKYXyiybqFnjfM3IQv f3BFbI9eYGoUlmAJVJnSTo+3jhd7yGXdTMvxNIMEAWrz1d9K6z/dkMZ6LhpOwC3SDFMt IUcA== MIME-Version: 1.0 X-Received: by 10.194.239.104 with SMTP id vr8mr8623416wjc.72.1443217326206; Fri, 25 Sep 2015 14:42:06 -0700 (PDT) Received: by 10.27.104.197 with HTTP; Fri, 25 Sep 2015 14:42:06 -0700 (PDT) In-Reply-To: <5605BACE.30803@gmail.com> References: <5605BACE.30803@gmail.com> Date: Fri, 25 Sep 2015 22:42:06 +0100 Message-ID: To: Stanislav Malyshev Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=001a11c1b4940ff6280520993804 Subject: Re: [PHP-DEV] Implementing Generics, and none scalar default properties. From: codekestrel@googlemail.com (Dominic Grostate) --001a11c1b4940ff6280520993804 Content-Type: text/plain; charset=UTF-8 I'm thinking primarily of the benefit to base or abstract classes. For base classes which expect certain properties be set, they are exposed to the danger of remaining unset if the derived class overrides the constructor without calling the parent. This solution is analogous to: class Foo { private $timestamp; private function __construct() { $this->timestamp = new DateTime(); $this->init(); } public function getTimestamp() { return $this->timestamp; } protected function init() { } } class Bar extends Foo { protected function init() { // pseudo constructor of Bar } } The problem now though is that derived classes of Foo are unable to do the same thing, whereas instantiated default properties would allow: class Foo { private $timestamp = new \DateTime(); public function getTimestamp() { return $this->timestamp; } } class Baz extends Foo { private $uuid = Uuid::create(); public function getUuid() { return $this->uuid; } } class Bar extends Baz { public function __construct() { // real constructor of Bar } } $bar = new Bar(); var_dump($bar->getTimestamp()); var_dump($bar->getUuid()); With Traits this is even more necessary, because every class (derived or base) would need to set the properties in each constructor. On 25 September 2015 at 22:21, Stanislav Malyshev wrote: > Hi! > > > ability to set default values of properties to instances of objects or > > calls to static methods or functions (expressions in general). > > That is what constructors are for. I.e. I can understand initializing > static properties (though it gives a lot of potential for weird race > conditions) but for non-statics constructor is exactly the place where > initialization should happen. > > -- > Stas Malyshev > smalyshev@gmail.com > --001a11c1b4940ff6280520993804--