Dynamically determine class

From: Date: Wed, 14 Sep 2005 07:56:51 +0000
Subject: Dynamically determine class
Groups: php.general 
Request: Send a blank email to php-general+get-222366@lists.php.net to get a copy of this message
Hi, I'm using PHP5 and try to do something like this: I have a base class called 'Entry' and a child class called 'ExtendedEntry'. I want to read all Entries to a certain topic from a database. While constructing the EntryObject I would like to look if Entry is a simple Entry or an ExtendedEntry. I want the new object to be an ExtendedEntry - if possible - and otherwise to be an Entry. It was not possible to return the object in the __construct() function and it was not possible to reassign $this with code like $this = new ExtendedEntry() also in the __construct() function. And I tried to always generate an ExtendedEntry with a flag, that says it is really Extended and a typecast afterwards, but I wasn't able to typecast to another object by (Entry) $extEntry. Any suggestions how to solve this problem without writing a generateEntry() function, that looks into the database and then calls the correct class type? thanks in advance, Norbert

« previous php.general (#222366) next »