joe Lovick wrote: > Hi, > forgive me if this is a complete noob question.... > What i want to do is create an object at runtime with members and > methods based on the result of queries from my db back end. is this (a)
Creating an object based on the result of a query shouldn't be a
problem. I'm not sure what logic you want to use exactly, but it is
more or less like this:
if ($some_condition) {
$obj = new FirstObject($arg1, $arg2);
$obj->prop1 = 'test';
$obj->prop2 = 1234.56;
} else {
$obj = new SecondObject($arg2);
$obj->prop1 = 'I am second object';
}
*HOWEVER* adding methods at runtime is only implemented in PHP4.
http://www.php.net/manual/en/function.aggregate-methods.php
Currently there is no explicit support for this in PHP5, although you
could probably make something usable with the __call() magic function.
> possible? or (b) something i should get out of my head and go and find a
> better solution?
Creating objects at runtime based upon certain conditions is sometimes
the best way. I don't suggest this in all cases mind you, but in some
cases this is quite useful (for example, database abstraction).
*HOWEVER* I do *not* recommend trying to aggregate methods dynamically.
I mean you can do this, but if possible just create a specialized class
that will do what you want instead of trying to add methods at runtime.
class DB {
function connect() {}
function query() {}
/** whatever other methods */
}
class MySQL extends DB {
}
class MySQL_4_1 extends MySQL {
function query() {}
function silly_function_only_relevant_to_MySQL_4_1() {}
}
--
Teach a man to fish...
NEW? | http://www.catb.org/~esr/faqs/smart-questions.html
STFA | http://marc.theaimsgroup.com/?l=php-general&w=2
STFM | http://www.php.net/manual/en/index.php
STFW | http://www.google.com/search?q=php
LAZY |
http://mycroft.mozdev.org/download.html?name=PHP&submitform=Find+search+plugins
signature.asc
Description: OpenPGP digital signature

