Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97266 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 97705 invoked from network); 2 Dec 2016 15:17:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Dec 2016 15:17:19 -0000 Authentication-Results: pb1.pair.com header.from=narf@devilix.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=narf@devilix.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain devilix.net designates 209.85.218.45 as permitted sender) X-PHP-List-Original-Sender: narf@devilix.net X-Host-Fingerprint: 209.85.218.45 mail-oi0-f45.google.com Received: from [209.85.218.45] ([209.85.218.45:36001] helo=mail-oi0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2E/CD-01781-E7091485 for ; Fri, 02 Dec 2016 10:17:18 -0500 Received: by mail-oi0-f45.google.com with SMTP id v84so268436012oie.3 for ; Fri, 02 Dec 2016 07:17:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devilix.net; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=f0ycED1F0uI7ShUQ4+AI5aezjue6AZRYA+Pmq0MoUh8=; b=AglijlMfV5fdXVEsm0eE2SA2UscR0ECXaKTA2OweFS4l//cZ4T4S9hyCV6DTgEUS/q BlfCGty2roJo44A39LYgsb/UeJH++jCtwr3zN7ny95/0eCENol5vLrgWfswX/qoCmBfi xGB3v/nUQe3HyrOVZbBPSYVNCPk2Qaau+F/9g= 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:from:date :message-id:subject:to:cc; bh=f0ycED1F0uI7ShUQ4+AI5aezjue6AZRYA+Pmq0MoUh8=; b=IpaWi5AE+jSHEU8fpzjEU7XawBGRKxKg6OVE0h5WqgfvuAafotsTbn2ViNtUUpQned VzWLoCndRkuswKovz4o/mph04XJ+TjrOsmxBjzXUBkc+DsAzR12diz/oez1SYlzao+dc h2bOhW2JJTRZD0RsZtt8r4GHulASDEzZGpf3Z13VkM3i6ML+PHQ2Crqcu0JgU7dnNXUr iI8Em6PeBBZcifPBzXiBmVdXX0rnWL+LloZKJ5LBRXd7b14JJisAuVN1lP6bZOmE93JU 8fLGEqBYcYE/Q3cV+mFk9kvl/IS73nM57ZVEO85x+jhS/EAcMqBJyjOkkpjE8Py9C3rl A4iw== X-Gm-Message-State: AKaTC01F8lxhUEThtw8U1VeDT2LtloKT6PRclNb1ySEWzIi5RSbRomN0LDtBhQ3PmYqj8L95N8AvFcc0Ayh/Rw== X-Received: by 10.157.8.46 with SMTP id 43mr25021126oty.54.1480691835449; Fri, 02 Dec 2016 07:17:15 -0800 (PST) MIME-Version: 1.0 Received: by 10.202.252.88 with HTTP; Fri, 2 Dec 2016 07:17:14 -0800 (PST) In-Reply-To: References: Date: Fri, 2 Dec 2016 17:17:14 +0200 Message-ID: To: Alex Bowers Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=94eb2c039f88dfdd350542ae6ec1 Subject: Re: [PHP-DEV] Re: [Concept] Magic Casting From: narf@devilix.net (Andrey Andreev) --94eb2c039f88dfdd350542ae6ec1 Content-Type: text/plain; charset=UTF-8 On Fri, Dec 2, 2016 at 4:53 PM, Alex Bowers wrote: > > On 2 December 2016 at 14:46, Andrey Andreev wrote: > >> A magic method is essentially an implicit interface ... >> The interface itself does nothing. But when it is implemented, the engine >> will know that the class constructor is public and accepts a single >> parameter - thus, also knowing that it could try to do a new >> ClassName($yourParameterHere) >> > > The interface would not work though, because there should be some logic in > place too. For example, if you accept, but convert an array, or an array of > arrays etc differently. Or would this be logic that should be placed inside > of the constructor, and the castable will just offload to a new > construction? > There's no logic in constructing differently from the same input type. If you handle an array passed to __cast() differently to one passed to __construct(), your design is inconsistent, to say the least. The only reason I can think of wanting to do that is if you'd like to magically create dependencies to pass to the constructor. For example: class Foo { public function __construct($scalar, Bar $bar) { // whatever } public static function __cast($var) { $bar = new Bar(); return new static($var, $bar); } } However, that is obviously tight-coupling at least one of your dependencies. I believe most people on the list would agree that this is not a good thing, but even if that wasn't the case - you could still do it in the constructor if you really wanted to: public function __construct($scalar, Bar $bar = null) { if ( ! isset($bar)) { $bar = new Bar(); } } Honestly, I don't see how a new method is in any way beneficial. Cheers, Andrey. --94eb2c039f88dfdd350542ae6ec1--