Jump to content

new class from return value of a method


flolam

Recommended Posts

Hi

I have a problem with the following:

<?php
class ClassA {
  public $propertyClassName = "ClassB";

  public function methodClassName() {
    return "ClassB";
  }
}

class ClassB {
public function bmethod() {
	echo "great!";
}
}

//works
$a = new ClassA();
$b = new $a->propertyClassName();
$b->bmethod();

//doesn't work
$a = new ClassA();
$b = new $a->methodClassName();
$b->bmethod();
?>

Of course I could do

<?php
$a = new ClassA();
$className = $a->methodClassName();
$b = new $className;
$b->bmethod();
?>

but isn't there a way to do this without saving the method's return to a variable?

Thanks in advance

flolam

Link to comment
https://forums.phpfreaks.com/topic/229029-new-class-from-return-value-of-a-method/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.