Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66181 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 3652 invoked from network); 25 Feb 2013 10:20:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Feb 2013 10:20:42 -0000 Authentication-Results: pb1.pair.com smtp.mail=inefedor@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=inefedor@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.43 as permitted sender) X-PHP-List-Original-Sender: inefedor@gmail.com X-Host-Fingerprint: 209.85.215.43 mail-la0-f43.google.com Received: from [209.85.215.43] ([209.85.215.43:62150] helo=mail-la0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 97/D3-10787-9FA3B215 for ; Mon, 25 Feb 2013 05:20:42 -0500 Received: by mail-la0-f43.google.com with SMTP id ek20so2511204lab.30 for ; Mon, 25 Feb 2013 02:20:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:content-type:to:subject:references:date:mime-version :content-transfer-encoding:from:message-id:in-reply-to:user-agent; bh=T+XYSuwAUGJmp99v3I2R1oxR/kfLQ7tO1VCo3BsoF5w=; b=BnB+B2OfRvZc2OLNoPUtS3GULGKtpW6d7gXNAqQhdEwD6wo6aiN1hENR3F/ZMrMZM6 QRFkvKCyIUVjefhPACqBmwn84EYK+19hvIU/sCCXrZUQcCbmTxwzT2vDbJXMsHPd6dZs RTCMNVCYyEGDnLMpgnZv2ibqNSfECEkwWlHy4mAK+zIk4jL5CWF9glITWxwt0IALtfPk /Y4szvtX+Fq89RLGRY8VCDck5G6qJbFv6LjS7FppbTo4C1rp6cRK6/x09sAo+wwNmg4o v0p3GU+xTyjtrKWSbG2cTBfA24ppD/LVO2nY6Vta6bP63UOFxG+Z+X5uAAoLu8Tx1vrG Uj0g== X-Received: by 10.152.45.140 with SMTP id n12mr9433107lam.36.1361787638421; Mon, 25 Feb 2013 02:20:38 -0800 (PST) Received: from nikita2206-n56vj ([217.174.184.92]) by mx.google.com with ESMTPS id xw14sm6585064lab.6.2013.02.25.02.20.37 (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 25 Feb 2013 02:20:37 -0800 (PST) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: internals@lists.php.net, "Jens Riisom Schultz" References: <01F30F77-B22D-40CE-ADF1-AC1C488FE39D@me.com> Date: Mon, 25 Feb 2013 14:20:36 +0400 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: <01F30F77-B22D-40CE-ADF1-AC1C488FE39D@me.com> User-Agent: Opera Mail/12.14 (Linux) Subject: Re: [PHP-DEV] Late FQCN resolution using ::class From: inefedor@gmail.com ("Nikita Nefedov") On Mon, 25 Feb 2013 14:00:04 +0400, Jens Riisom Schultz wrote: > Hi everybody, > > I have read up on this, and done some testing. > > First up, my findings with PHP5.5 alpha5: > > namespace spacy; > > class classy { > public static function fqcn() { > /* This works but is not useful enough: */ > //return self::class; > > $me = 'classy'; > > /* This just doesn't work, but I wish it did: */ > //return $me::class; > > /* This simply does not work as expected: */ > return eval("return $me::class;"); > /* Output: "classy" - Expected output: "spacy\classy" */ > } > } > ?> > > I'm trying to late resolve a class name contained in a variable to the > FQCN. I understand that this is hard (maybe even impossible) with the > current implementation, because class name resolution happens compile > time, but eval("return $me::class;") simply returns something that is > weird. > > I guess what I'm trying to ask is whether it would be impossible to > support late FQCN resolution in any way? It would be very useful for > frameworks to be able to do this. > > - Jens Riisom Schultz > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > Hi Jens, Here's what happened in your code: When you invoked fqcn(), you created var $me = "classy"; Then you tried to invoke this code in eval: "return classy::class;" But when php evals code, it's like including another file. So it executes the code without any namespace (it's in global namespace), and php didn't discover class with name classy (there's only spacy\classy) yet, so it tries to resolve all your "use" statements (but you didn't write any) and then just gives you "classy", it didn't throw any error just because it tried to delay autoloading of this class as far as possible, if would do eval("return new $me;") then you would get fatal error.