Edit report at https://bugs.php.net/bug.php?id=61382&edit=1

 ID:                 61382
 Comment by:         anon at anon dot anon
 Reported by:        todesw1nd at hotmail dot com
 Summary:            need a function class_rename
 Status:             Open
 Type:               Feature/Change Request
 Package:            SPL related
 Operating System:   independent
 PHP Version:        5.4.0
 Block user comment: N
 Private report:     N

 New Comment:

You don't need this. Give all the derived (wrapper) car classes different 
names, then stick the name of the selected car in a string variable and 
instantiate it from the variable.

$class_name = 'Sports_Car';
...
$car1 = new $class_name;


Previous Comments:
------------------------------------------------------------------------
[2012-03-14 06:40:23] todesw1nd at hotmail dot com

tiny error in code example:

class Wrapper_Car extends Base_Car <- this should be more correct

the class name for all "real" cars is Wrapper_Car, even if i buy a third one

------------------------------------------------------------------------
[2012-03-14 06:37:12] todesw1nd at hotmail dot com

Description:
------------
this is OOP related and maybe targets to AOP.

Since i found a quick & dirty solution for myself using eval() in my framework 
it is not urgent, but would be nice to have to no longer need it as it is not 
used on any other parts currently.

The problem itself:

Lets say we have two cars and don't care about which one we drive, we just want 
to use one of it and a friend should decide which one by selecting it.

In PHP i now have to use a wrapper class for each of the two cars with the same 
name, e.g. Wrapper_Car and a base class that is extended with their 
functionality, e.g. Base_Car. I do have a mess now as different cars can't have 
the same file name due to the default autoloader.

Test script:
---------------
file base_car.php:

abstract class Base_Car
{
    abstract public function drive();
}

file wrapper_car1.php:

class Wrapper_Car
{
    public function drive
    {
        // Lets drive it ^^
    }
}

file wrapper_car2.php:

same content as wrapper_car1.php but slightly other implementation inside the 
methods of the class

Expected result:
----------------
I would like to have a function like this:

class_rename($class_name, $class_newname);

So i can autoload the car class i want to use, e.g. wrapper_car1.php and have 
the class called Wrapper_Car1 and then rename it to Wrapper_Car as it is the 
selected car i want to drive with.

Important: It would be ok if the autoloader tries to load the first param 
$class_name if it does not exist, but it should never ever do it for the second 
parameter as e.g. class_alias does it currently and is therefore useless for 
this case.

Actual result:
--------------
As said before i currently use a one liner eval that sets the namespaces and 
creates the renamed class based on the earlier loaded one to emulate this 
behavior and i think that is little dirty :]

Thanks for reading this and it would be awesome to have such a functionality in 
a later PHP version. If you see it from another point of view it would be nice 
to see why.


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61382&edit=1

Reply via email to