[PHP] Re: Dynamic/runtime object creation
Thanks for your help Jason, yes aggregating methods was what i had in mind, but now as i explore it as a option i realise that it would work best is if their was a way for me to pull my object out of the database variables, data, methods and all and then instantiate them some how, with out any of the code being prewritten. a bit like create_function() for lambada type functions, but in this case for my more general object code... and searching the hell out of all these formums gives me the classkit functions, so i guess i will try and install and give them a try... anyhow cheers joe -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Dynamic/runtime object creation
joe Lovick wrote: Thanks for your help Jason, yes aggregating methods was what i had in mind, but now as i explore it as a option i realise that it would work best is if their was a way for me to pull my object out of the database variables, data, This is certainly do-able. You serialize() an object and store this in the DB... methods and all and then instantiate them some how, with out any of the code ... however, you *must* include a class definition for an object before the object can be instantiated. When an object gets serialized it is only the object's properties (*not* its methods) that become serialized. being prewritten. a bit like create_function() for lambada type functions, but in this case for my more general object code... and searching the hell out of all these formums So long as you have the (potential) class definitions already included, you can use unserialize() to create the object (whatever type it happens to be). You don't even need to know what kind of object it's going to be. :) gives me the classkit functions, so i guess i will try and install and give them a try... anyhow cheers joe -- Teach a man to fish... NEW? | http://www.catb.org/~esr/faqs/smart-questions.html STFA | http://marc.theaimsgroup.com/?l=php-generalw=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=PHPsubmitform=Find+search+plugins signature.asc Description: OpenPGP digital signature
[PHP] Re: Dynamic/runtime object creation
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-generalw=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=PHPsubmitform=Find+search+plugins signature.asc Description: OpenPGP digital signature