|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61382 need a function class_rename
Submitted: 2012-03-14 06:37 UTC Modified: 2012-03-15 11:01 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: todesw1nd at hotmail dot com Assigned:
Status: Closed Package: SPL related
PHP Version: 5.4.0 OS: independent
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: todesw1nd at hotmail dot com
New email:
PHP Version: OS:


 [2012-03-14 06:37 UTC] todesw1nd at hotmail dot com
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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-14 06:40 UTC] 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 15:29 UTC] anon at anon dot anon
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;
 [2012-03-14 17:49 UTC] todesw1nd at hotmail dot com
if that also works for static requests i might use it using a fabric pattern, thought it won't.
 [2012-03-15 11:01 UTC] todesw1nd at hotmail dot com
-Status: Open +Status: Closed
 [2012-03-15 11:01 UTC] todesw1nd at hotmail dot com
no longer needed, found a workaround without eval :)
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Jan 26 09:03:33 2022 UTC