php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39667 class extends should parse a T_VARIABLE
Submitted: 2006-11-28 22:38 UTC Modified: 2006-11-28 22:41 UTC
From: codeslinger at compsalot dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.2.0 OS: Any
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2006-11-28 22:38 UTC] codeslinger at compsalot dot com
Description:
------------
Feature Request:

The construct: class foo2 extends foo1{}

will only accept a T_STRING for foo1

But what is highly desired is to also accept a T_VARIABLE.

class $strFoo2 extends $strFoo1{}

Should be a valid construct.  This would enable some very powerful techniques, such as object encapsulation and psudo-nesting.


Reproduce code:
---------------
//Goal: To encapsulate a multiply subclassed object

class foo1{
    function foo1{echo "foo1\n";}
}
$MyClass = "foo1";


class foo2 extends $MyClass{
    function CoolExtention1{echo "Cool Extension #1\n";}
}
$MyClass = "foo2";


class foo3 extends $MyClass{
    function TotallyCoolExt{echo "Totally Cool Ext #2\n";}
}
$MyClass = "foo3";

//////////////
//
// The program now sees an encapsulated class
//   without having to know anything in advance.
//   and without having to be modified when changes
//   are made to the object.

$MyObj = new $MyClass;


Expected result:
----------------
/*******************

   It's a question of being able to isolate the dependancies
   I don't want to hard-code the class names because I have
   multiple alternative extendors for the base class.  It's 
   basically a bunch of building blocks that I assemble as 
   needed to create a custom class, giving me a common 
   interface but with different functional implementations.  
   I want the consuming program to see a single encapsulated 
   object class. Aggrate does not do inheritance.

   PHP5 has Great!!!  tools for working with objects and 
   functions.  But Encapsulation is one area it lacks and
   which could easily? be added by enabling the class
   definition to parse T_VARIABLEs and then using the trick
   above.  This gives you the functional equivlent of nested 
   classes as long as you manage your references carefully.

   I did manage to get this working using eval, but it's an
   ugly hack when dealing with a class of any complexity.
   \$everywhere.  I also have some concerns about what the 
   eval approach may be doing to runtime performance versus 
   what could be done at cachable compile time.

   /////////////////////////////////////////////////////////
   Taking it to the next level...a trick nobody else can do?

class $strfoo2 extends $MyClass{
    function {$strfoo2}{echo "Awesome $strfoo2 class!\n";}
}

*******************/



Actual result:
--------------
Parse error: unexpected T_VARIABLE, expecting T_STRING


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-28 22:41 UTC] tony2001@php.net
Unfortunately, this is not possible, because in most cases classes are declared in compile time, when the value of this variable is unknown.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 21:01:29 2024 UTC