Quick question,

Is there any way to extend one class from a class whose name is specified in a variable. I.e.

$baseClassName = "Class2";
class Class1 extends $baseClassName
{
...
}

You can do things like '$a = new $baseClassName();', and this doesn't seem so different, but is this possible?

Thanks

    Why do you want to do this in the first place? I do not think that it is possible, and I also do not see a reason for it. Chances are you are looking to use composition instead of inheritance.

      I can't imagine why you'd want to, but I suppose it could be done with eval():

      $classString = <<<EOD
      class Class1 extends %s
      {
         // class definition here
      }
      EOD;
      $parent = 'SomeClass';
      eval(sprintf($classString, $parent));
      
        Write a Reply...