We all know the infamous "cannot redeclare class" error. Is there any method to overcome this and actually declare a new class with the same name, or is this impossible in PHP 5?
相关问题
- Views base64 encoded blob in HTML with PHP
- how to define constructor for Python's new Nam
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Keeping track of variable instances
There may be a way using some obscure extension, but in basic standard PHP, as far as I know, no.
You can, however, always extend an existing class and - maybe, depending on your scenario - "smuggle" an instance of that extended class into the application you're working on.
Perhaps a more modern answer for 2016, it looks like there are now at least 3 options:
Option 1. Using runkit
Ref: http://php.net/manual/en/function.runkit-class-emancipate.php
bool runkit_class_emancipate ( string $classname )
"Convert an inherited class to a base class, removes any method whose scope is ancestral"
Though I haven't tested this myself, it seems like a viable option if you have control over the extensions loaded. The drawback here would be that you lose all ancestral methods.
Option 2. Using runkit, again
Ref: http://php.net/manual/en/function.runkit-method-redefine.php
bool runkit_method_redefine ( string $classname , string $methodname , string $args , string $code [, int $flags = RUNKIT_ACC_PUBLIC ] )
"Dynamically changes the code of the given method"
This solves the problem of option 1 if your goal is to tweak a method or two on the base class.
Option 3. Implement an Autoloader
Ref: http://php.net/manual/en/function.spl-autoload-register.php
bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
"Register given function as __autoload() implementation"
This is personally my favorite of the 3, because:
This is also one for which I do have some experience. An example implementation is as follows:
AFAIK, redeclaring exiting functions or classes is not possible in PHP.
If you could tell, what you are trying to do, maybe there's another solution ...
It is impossible. Depending on the use case, namespaces, like jpfuentes2 mentioned, might work for you.
One hack is to implement a custom new "operator".
Example:
Another hack is to use runkit to re-implement a class.
Basically you cannot redeclare a class. But if you really want to, you can. :) Everything is possible. Need a class, that changes it's structure dynamically? You can use magic method __call and pattern State.
There is also a PHP toolkit to play with classes dynamically: http://php.net/manual/en/book.runkit.php
I know that redeclaring class and its methods is possible in Ruby (and I would consider it as a mistake in a language design).
Basically we can't redeclare a class in PHP
directly
. If you are in need to get redeclare a class in php, then I suggest you to write that class in a separate file and userequire_one
to call that file to the desired page. It's as follows:Page1.php
Page2.php
Now it will work as you desired. It worked for me.
The output will be as follows :