spires Posted June 20, 2011 Share Posted June 20, 2011 Hi I'm doing some OOP But can not find a way to make the class name dynamic. I'm sure there must be a way of doing it, as I use this a lot, i'm sure other must also. This is what I have: $wrapperLCID = $newListWrappers->list_category_id; $wrapperName = $newListWrappers->name; if($wrapperLCID==1){ $newParent = Business_solutions_cat::find_all(); }elseif($wrapperLCID==2){ $newParent = Blackberry_cat::find_all(); }elseif($wrapperLCID==3){ $Pcategory = Case_studies_cat::find_all(); }elseif($wrapperLCID==4){ $Pcategory = Stand_alone_cat::find_all(); }elseif($wrapperLCID==5){ $newParent = Reasons_cat::find_all(); }elseif($wrapperLCID==6){ $newParent = Charity_cat::find_all(); } This Is What I want: $wrapperLCID = $newListWrappers->list_category_id; $wrapperName = $newListWrappers->name; $newParent = $wrapperName::find_all(); Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/239916-oop-class-naming/ Share on other sites More sharing options...
KevinM1 Posted June 20, 2011 Share Posted June 20, 2011 You need to use the Factory Method here: abstract class WrapperFactory { public static function GetById($id) { switch($id) { case 1: return new Business_solutions_cat(); break; case 2: return new Blackberry_cat(); break; // etc. } } } $wrapper = WrapperFactory::GetById($id); $newParent = $wrapper::findAll(); Quote Link to comment https://forums.phpfreaks.com/topic/239916-oop-class-naming/#findComment-1232388 Share on other sites More sharing options...
AbraCadaver Posted June 20, 2011 Share Posted June 20, 2011 The code you posted should work in PHP 5.3. This is the way in earlier versions: $newParent = call_user_func(array($wrapperName, 'find_all')); Quote Link to comment https://forums.phpfreaks.com/topic/239916-oop-class-naming/#findComment-1232389 Share on other sites More sharing options...
spires Posted June 20, 2011 Author Share Posted June 20, 2011 $newParent = call_user_func(array($wrapperName, 'find_all')); Works perfect Thanks for that. Do you know when 5.3 will be stable on all hosting? I'm only on 5.2.17 currently. Thanks Again Quote Link to comment https://forums.phpfreaks.com/topic/239916-oop-class-naming/#findComment-1232395 Share on other sites More sharing options...
AbraCadaver Posted June 20, 2011 Share Posted June 20, 2011 Do you know when 5.3 will be stable on all hosting? I'm only on 5.2.17 currently. I have no idea. We still get questions about things not working on PHP 4! Quote Link to comment https://forums.phpfreaks.com/topic/239916-oop-class-naming/#findComment-1232397 Share on other sites More sharing options...
spires Posted June 20, 2011 Author Share Posted June 20, 2011 Hi. Thanks again. But what If I want to pass the class an ID, or multiple variables? E.G: find_by_id(10); Quote Link to comment https://forums.phpfreaks.com/topic/239916-oop-class-naming/#findComment-1232413 Share on other sites More sharing options...
AbraCadaver Posted June 20, 2011 Share Posted June 20, 2011 Hi. Thanks again. But what If I want to pass the class an ID, or multiple variables? E.G: find_by_id(10); call_user_func_array() Quote Link to comment https://forums.phpfreaks.com/topic/239916-oop-class-naming/#findComment-1232415 Share on other sites More sharing options...
spires Posted June 20, 2011 Author Share Posted June 20, 2011 Do you mean like this? $newChild = call_user_func_array(array($className, 'find_by_id'), array($LPlistID)); Quote Link to comment https://forums.phpfreaks.com/topic/239916-oop-class-naming/#findComment-1232418 Share on other sites More sharing options...
redixx Posted June 20, 2011 Share Posted June 20, 2011 Do you mean like this? $newChild = call_user_func_array(array($className, 'find_by_id'), array($LPlistID)); Yup. See the manual for more information Quote Link to comment https://forums.phpfreaks.com/topic/239916-oop-class-naming/#findComment-1232458 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.