Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66988 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99107 invoked from network); 5 Apr 2013 14:35:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Apr 2013 14:35:06 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.212.46 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.212.46 mail-vb0-f46.google.com Received: from [209.85.212.46] ([209.85.212.46:50723] helo=mail-vb0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 96/92-20511-911EE515 for ; Fri, 05 Apr 2013 09:35:06 -0500 Received: by mail-vb0-f46.google.com with SMTP id 11so2163989vbe.5 for ; Fri, 05 Apr 2013 07:35:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type:x-gm-message-state; bh=pyQX1hVxTCSH82eb9CczjYJWyS0egN1uhd+tv/+kojQ=; b=QB4CJN26TUX8t18MwlwzJjSmu5kv2K1Wwk3F+PbodO0Pm0ndNem4Ymu10NanKsNJfO W2Rxfeitx3jusGgxfE2OeWuRZwk7j6BZxk9zC23FrYlRN1hf03BYH6Wjuhfbp0HJHJv2 2OqxWDg0LhSMfP75vHJRjqs9BbTc84B5ngJni6FvOM3s4jSs9nIw798p0IW6MpBtBVNC 21qQ/NiKWY6ssS7i7wCIKNbUduR1SEsviuQv24csKgZqcJecJGeMqtk5SGXEP3hOl367 kjkqkIFhlT+LRtBj80xFsmX+ZOoPJkqWYG5wfOAwdgvDtQOV+iPgZwPYRCIOL+HL6EoO Jbeg== MIME-Version: 1.0 X-Received: by 10.58.144.133 with SMTP id sm5mr8371107veb.23.1365172503562; Fri, 05 Apr 2013 07:35:03 -0700 (PDT) Received: by 10.58.28.134 with HTTP; Fri, 5 Apr 2013 07:35:03 -0700 (PDT) In-Reply-To: References: Date: Fri, 5 Apr 2013 10:35:03 -0400 Message-ID: To: ALeX Cc: Richard Bradley , Benjamin Eberlei , PHP internals Content-Type: multipart/alternative; boundary=047d7b675b2621e11404d99dfe4d X-Gm-Message-State: ALoCoQnRPg9Dhs4lyTtmMooYbxjYFTVIcGeLn8WYum0S2+JlNsnwmdtFYuMygs7lx3SUkPPkgXGo Subject: Re: [lists.php] Re: [PHP-DEV] a couple of thoughts on the DateTime type debate From: rasmus@mindplay.dk (Rasmus Schultz) --047d7b675b2621e11404d99dfe4d Content-Type: text/plain; charset=ISO-8859-1 > On the other hand, I would just use an array. (without any "magic" > like methods on structs, yes you would have to write plain functions > and not use OOP like methods). Yeah, that's what people are doing right now - the problem with that, is you have the class-name referenced on every call, e.g.: abstract class Color { public static function create($r, $g, $b) { return array('r'=>$r, 'g'=>$g, 'b'=>$b); } public static function mix(array $base, array $mix, $amount) { return array( 'r' => $base['r'] * $amount + $mix['r'] * (1 - $amount), 'g' => $base['g'] * $amount + $mix['g'] * (1 - $amount), 'b' => $base['b'] * $amount + $mix['b'] * (1 - $amount), ); } } $red = Color::create(1, 0, 0); $green = Color::create(0, 1, 0); $mix = Color::mix($red, $green, 0.5); The problem with this approach, is you have a static reference to the Color-class for every method-call. With structs you can accomplish the same thing without littering your code with static class-references. With pseudo-types implemented as arrays in this way, you also can't hope to have IDE support or static analysis, e.g. for properties... On Fri, Apr 5, 2013 at 10:00 AM, ALeX wrote: > > I imagine the implementation could be something along the lines of > checking > > for the '__struct' key when somebody attempts to use method-call syntax > on > > an array, invoking the appropriate method with $this referencing the > array > > you were using. > > > > The rest of the time, a struct, for all intents and purposes, is just an > > array. > > One thing I do not like about the "struct as array" is that you can > create "invalid" structs, in classes you could have all variables > private and check during set, but not here: "$array = ['r'=>1, > 'b'=>'yes', '__struct'=>'Color'];". > > Hmm... just an thought: why not make struct almost like a class except > that $this is a copy (on write) - modifying and returning $this would > be a new instance of that struct/class. That would give you > public/private/static/variables/methods/interfaces/..., but it would > lead to another type. > Or use a keyword to the class, e.g. "autoclone class Color {...", and > not the new name struct -> it would be clear that struct/classes use > the same namespace. > You maybe even could do "autoclone class DateTimeImmutable extends > DateTime {}" to create the immutable version. (I see no reason why an > "normal" class could not be extended into autoclone, but useless in > most cases though) > > On the other hand, I would just use an array. (without any "magic" > like methods on structs, yes you would have to write plain functions > and not use OOP like methods). > --047d7b675b2621e11404d99dfe4d--