Variable class extensions of a variable class in PHP - is this possible?

In PHP there is something like the following:

$blah = 'foo1'; class foo2 extends $blah { //... } class foo1 { //... } 

This gives an error.

I want to dynamically set $ blah so that I can extend any class I want.

Edit: The reason for this is because I wanted to use a function from another class in a related class. In the end, it would be something like:

 Final extends foo1 extends foo2 extends foo3 extends foo4 extends parent { ... } 

In the end, I decided to create an instance of another class inside the class and use it. Not the best options, because you both have 2 of the same classes, but it will not be used so often, so it will work for now.

+4
source share
9 answers

You assume that php runs from top to bottom, but this is not entirely true:

 <?php foo(); # works function foo(){ print "bar"; } 

 <?php foo(); #dies if( $i == 1 ) { function foo(){ print "bar"; } } 

 <?php $i = 1; if( $i == 1 ) { function foo(){ print "bar"; } } foo(); #works 

Now, although you can conditionally create classes:

 <?php class A { } class B { } if( false ){ class C extends B { public static function bar(){ print "baz"; } } } C::bar(); # dies 

You cannot create an instance at runtime from a variable:

 <?php class A { } class B { } $x = 'B'; if( false ){ class C extends $x { public static function bar(){ print "baz"; } } } C::bar(); ---> Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /tmp/eg.php on line 7 

There is a way to do this with Eval, but you really don't want to go there:

 <?php class A { } class B { } $x = 'B'; if( true ){ $code =<<<EOF class C extends $x { public static function bar(){ print "baz"; } } EOF; eval( $code ); } C::bar(); $o = new C; if ( $o instanceof $x ) { print "WIN!\n"; } --->barWIN! 

However, there is a more important question here:

Why the hell do you want to extend the class at runtime

Anyone who uses your code will want to hold you back and knock you out for it.

(Alternatively, if you are in a beating, do this trick)

+15
source

I don’t see how this would be particularly useful, but to answer your question ... no. There is no way to dynamically do this, because the generated class must be created before the variable is evaluated (if that makes sense).

Simply put: a class must exist before the code runs correctly.

+3
source

I guess this is for ease of maintenance, right? The class extension at runtime is really quite crazy.

 class SuperClassOne { /* code */ } class SuperClassTwo { /* code */ } class IntermediateClass extends SuperClassOne { /* empty! */ } class DescendantClassFoo extends IntermediateClass{ } class DescendantClassBar extends IntermediateClass{ } class DescendantClassBaz extends IntermediateClass{ } 

Then, when you want to change all your DescendantClass* classes, you just need to change what IntermediateClass extends:

 class IntermediateClass extends SuperClassTwo { } 
+2
source

If you don't have too many values ​​for $ blah, you can expand them in another file, and then require_once "classes/foo_$blah.php"

Otherwise, you are stuck with eval() ... good luck with that ... :)

+2
source

I tested something with definitions and barking:

 <?php define("INHERIT",A); class A{ public function bark(){ return "I'm A"; } } class B{ public function bark(){ return "I'm B"; } } class C extends INHERIT{} //main? $dog = new C(); echo $dog->bark(); ?> 

output:

Fatal error: class 'INHERIT' not found in D: \ sites \ inherit.php on line 15

so the answer to the question: "Are extensions of class variables possible?": None.

+1
source

I know this question was asked a long time ago, but the answer is relatively simple.

Assuming you want to extend the class foo if the class foo exists, or the class bar if it is not, you should use:

 if(!class_exists('foo')) { class foo extends bar { function __construct() { parent::__construct(); } } } class myclass extends foo{ //YOUR CLASS HERE } 
+1
source

Using PHP overload , you can do this to a certain extent.

 class variable_class { public $orginalBaseClass; public $orginalArgs; public function __construct() { // Get constructor parameters. $this->orginalArgs = func_get_args(); // Get class name from args or 3rd party source. $classname = 'stdClass'; // Pass along args to new class. $this->orginalBaseClass = new $classname($this->orginalArgs); } public function __call($name, $arguments) { // Pass all method calls to the orginalBaseClass. return call_user_func_array(array($this->orginalBaseClass, $name), $arguments); } } 

I use this template inside the Drupal module to prefetch data from the cache .

+1
source
 <?php class AAA { function go() { echo 'WORKS!'; } } $a = 'AAA'; use AAA as Eclass; class BBB extends EClass { } BBB::go(); ?> 
0
source

you should try $$

 $blah = 'foo1'; class foo2 extends $$blah { //... } class foo1 { //... } 
-1
source

All Articles