flolam Posted February 27, 2011 Share Posted February 27, 2011 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 More sharing options...
trq Posted February 27, 2011 Share Posted February 27, 2011 public function methodClassName() { return new ClassB; } } Link to comment https://forums.phpfreaks.com/topic/229029-new-class-from-return-value-of-a-method/#findComment-1180540 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.