views:

2128

answers:

5

Is something like the following possible in PHP?

$blah = 'foo1';

class foo2 extends $blah {
    //...
}

class foo1 {
    //...
}

This gives an error.

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

Edit: The reason for wanting to do this because I wanted to use a function out of another class in a related class. In the end it would have been something like:

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

In the end I decided to instantiate the other class within the class and use it. Not the best options because they both you 2 of the same classes, but this won't be used that often, so it will work for now.

+3  A: 

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

To put it simply: The class must exist before the code can be properly executed.

Jeff Hubbard
My feeling was the same. But with PHP, you never know... ;-)
Tomalak
As Kent Fredric said, it is possible. Not pretty but possible.
Darryl Hein
+9  A: 

You're assuming here php executes top to bottom, but it doesn't quite work like that:

<?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 cant instantiate one 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 it 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 would you want to extend a different class at runtime

Anybody using your code will want to hold you down and whip you for that.

( Alternatively, if you're into whipping, do that eval trick )

Kent Fredric
A: 

I assume that this is for ease-of-maintenance, right? Extending a class at run time really is pretty 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 have to change what the IntermediateClass extends:

class IntermediateClass extends SuperClassTwo { }
nickf
Bad use of single line comments. The closing brackets } are commented!
luiscubal
+1  A: 

If you don't have too many values for $blah, you could extend each one in a different file then require_once "classes/foo_$blah.php"

Otherwise, you're stuck with the eval() solution... good luck with that... :)

Greg
A: 

you should have tried $$

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

class foo1 {
    //...
}
Matthew
does that work?
Darryl Hein