Jump to content

OOP Class Naming


spires

Recommended Posts

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 :)

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.