Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66619 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 39663 invoked from network); 14 Mar 2013 16:43:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Mar 2013 16:43:00 -0000 Authentication-Results: pb1.pair.com smtp.mail=ekneuss@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ekneuss@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.181 as permitted sender) X-PHP-List-Original-Sender: ekneuss@gmail.com X-Host-Fingerprint: 209.85.216.181 mail-qc0-f181.google.com Received: from [209.85.216.181] ([209.85.216.181:49199] helo=mail-qc0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 46/75-07586-21EF1415 for ; Thu, 14 Mar 2013 11:42:59 -0500 Received: by mail-qc0-f181.google.com with SMTP id a22so1098114qcs.40 for ; Thu, 14 Mar 2013 09:42:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=pS8hUPvRZLvrlNQONrPtwJYQ3qEnMtD0b1ybPpsoc08=; b=ryQIz+wCbZckfX8AnJbMDhKeR7wxj7ia/mLSXffULQrZLo83iQpH+Ef6aWztd6ZGzD +u773h3KsQz/ntc4U8vefo1N3rkzs4GFPZxNWkgoWz3FdzLaxzJRjmE8xOq+6n07npFS 73bHp8Cr2evGtnQGiVtuwSRYUaeFp9/UdoCik7uP4j09+JnRcKm2cS3NS6LjLs8zY2ql 1e32EqSXecM6y9Ny2GZ5Pbu6btoT+JxoT04LIKFcPz9HFk1Dj7wK+N6TXDMQm6hUEs5V Ma097q38OeCgF11HmiZnMiPS0Jfq5WxNuerS+OmA4Rh1NZ7Cyw+VVt+/Ah2xj6LsfZc/ RJTg== X-Received: by 10.49.127.15 with SMTP id nc15mr3197693qeb.61.1363279375421; Thu, 14 Mar 2013 09:42:55 -0700 (PDT) MIME-Version: 1.0 Sender: ekneuss@gmail.com Received: by 10.49.133.42 with HTTP; Thu, 14 Mar 2013 09:42:34 -0700 (PDT) In-Reply-To: References: Date: Thu, 14 Mar 2013 17:42:34 +0100 X-Google-Sender-Auth: 1mNfSIA3LzBSj4Q7LLAqSXwYqvU Message-ID: To: Rasmus Schultz Cc: PHP internals Content-Type: multipart/alternative; boundary=047d7b6dc3ace7ede204d7e5369d Subject: Re: [PHP-DEV] static type-references From: colder@php.net (Etienne Kneuss) --047d7b6dc3ace7ede204d7e5369d Content-Type: text/plain; charset=UTF-8 Dear Rasmus, On Thu, Mar 14, 2013 at 5:26 PM, Rasmus Schultz wrote: > Hey List, > > What do you think about adding a typeof() operator to PHP? > > It could go something like this: > > class User > { > public $name; > } > > /** > * @var ReflectionClass $user_type > * @var ReflectionProperty $user_name_property > */ > > $user_type = typeof(User); > > $user_name_property = typeof(User::$name); > > Mind you, this is an operator and not a function - User and User::$name are > interpreted as symbols. > > This would allow for static analysis of class and property-references. > > Why? Because you can't use static analysis on code like this: > > $user_type = new ReflectionClass('User'); > > $user_name_property = new ReflectionProperty('User', 'name'); > > 'User' and 'name' are just strings, and strings are dead matter when it > comes to static analysis. > It is certainly possible for static analyses to reason about literal strings. Handling reflective code is not that hard when its arguments are literals. > > Most form builders and ORMs (and other reflective meta-programming > components) require references to properties or classes, e.g.: > > $form->textInputFor($user, 'name'); > > There is no knowledge that 'name' actually refers to the symbol User::$name > and hence to way to perform static analysis or use refactoring tools on > this code. > > Compare to: > > $form->textInputFor($user, typeof(User::$name)); > To me this code makes no sense. I expect typeof to return a type, since we have no first-class types in PHP, I expect it to return a string representing the type. If I understand your example, here it returns a Reflection* ? What about typeof($a->b) or even typeof($a->$foo) ? I assume both of them would be forbidden ? > This code can be analyzed and refactored, because the reference to the > User::$name property is now literal. > > Difficulty to statically analyze code comes from its dynamic nature, to me typeOf(User::$name) is no less dynamic than new ReflectionProperty("User", "name"). > Actually having to type out typeof() may not be the most elegant approach, > and using this syntax it does resemble a function - having a more distinct > syntax might be better. > > But those things aside, what do you think about having a way to statically > reference types and members? > Thanks, > Rasmus Schultz > -- Etienne Kneuss --047d7b6dc3ace7ede204d7e5369d--