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
https://forums.phpfreaks.com/topic/239916-oop-class-naming/
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
https://forums.phpfreaks.com/topic/239916-oop-class-naming/#findComment-1232388
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.