laravel - PHP best practice for action based on an object type -


i have php/laravel best practice question.

here scenario: have object "type" property in it. type property set integer of kind.

depending on "type" integer in object, different actions have happen.

what best way process object? trying desperately think of way avoids using whole bunch of if/else statements feels wrong , ugly way approach this.

i.e. don't want this:

if($obj->type == 1) {     //process actions type 1 } else if($obj->type == 2){     //process actions type 2 } 

would appreciate advice.

thanks!

thanks @ryan vincent found resource (https://sourcemaking.com/design_patterns/strategy/php) , changed strategy design pattern bit. avoiding hard-coded type values check how dynamic class loading done in strategycontext::__construct method. new class instance initiated $type variable name. class names should strings in php way forces types strings not numbers.

different original example in article, attached strategycontext book object , wrap methods strategy have better use of book object. unfortunately if business logic in code somehow need hardcode it. method don't hardcode each type need create strategy class each type. in example have strategycaps , strategystars , strategyexclaim strategies. our types limited caps, stars , exclaim.

i didn't try piece of code in production environment can have starting point via example.

also dynamic loading, can benefit question too.instantiate class variable in php?

hope helps,

<?php interface strategyinterface {     public function showtitle($title);     public function showauthor($author); }  class strategycontext  implements strategyinterface {     private $strategy = null;       public function __construct($type) {          //dynamic class loading per type         $classname="strategy{$type}";         if(class_exists($classname)) {           $this->strategy = new $classname();         } else {           throw new exception("strategy not found", 1);         }      }     public function showtitle($title) {       return $this->strategy->showtitle($title);     }      public function showauthor($author) {       return $this->strategy->showauthor($author);     } }   class strategycaps implements strategyinterface {     public function showtitle($title) {         return strtoupper ($title);     }      public function showauthor($author) {         return strtoupper ($author);     } }  class strategyexclaim implements strategyinterface {     public function showtitle($title) {         return str_replace(' ','!',$title);     }     public function showauthor($author) {         return str_replace(' ','!',$author);     } }  class strategystars implements strategyinterface {     public function showtitle($title) {         return str_replace(' ','*',$title);     }          public function showauthor($author) {         return str_replace(' ','*',$author);     }  }  class book {     private $author;     private $title;     private $strategy;      function __construct($strategy, $title_in, $author_in) {         $this->strategy = new strategycontext($strategy);         $this->author = $author_in;         $this->title  = $title_in;     }     function getauthor() {         return $this->strategy->showauthor($this->author);     }     function gettitle() {         return $this->strategy->showtitle($this->title);     }     function getauthorandtitle() {         return $this->gettitle() . ' ' . $this->getauthor();     } }  writeln('begin testing strategy pattern'); writeln('');  $type = 'caps'; $book = new book($type, 'php cats','zeev suraski'); writeln($book->getauthorandtitle());   $type = 'exclaim'; $book = new book($type, 'php unicorns','rasmus lerdorf'); writeln($book->getauthorandtitle());   $type = 'stars'; $book = new book($type, 'php ponys','andi gutmans'); writeln($book->getauthorandtitle());   function writeln($line_in) {   echo $line_in.php_eol; } 

update:

so if using orm can assume book model class. don't have knowledge eloquent , how handles data binding etc. i'll make simple can. assume can use constructor binded data database.

keep strategycontext , actual strategy classes -where biz logic coded- service , use dependency injection while finding out strategy use. way can bind strategies depending on type variable, model object.

updated version of book class,

class book {     private $author = "zeev suraski";     private $title = "php cats";     private $strategy;     private $type = 'caps';      function __construct() {         $this->strategy = new strategycontext($this->type); //dependency injection here     }      function getauthor() {         return $this->strategy->showauthor($this->author);     }      function gettitle() {         return $this->strategy->showtitle($this->title);     }      function getauthorandtitle() {         return $this->gettitle() . ' ' . $this->getauthor();     }      function settype($type) {       $this->type = $type;     }      function setstrategy($type=null) {       if($type==null) {         $this->strategy = new strategycontext($this->type); //dependency injection here       } else {         $this->strategy = new strategycontext($type); //dependency injection here         $this->settype($type);       }     }  }   writeln('begin testing strategy pattern'); writeln('');  $book = new book(); writeln($book->getauthorandtitle());   $type = 'exclaim'; $book->settype($type); $book->setstrategy(); writeln($book->getauthorandtitle());   $type = 'stars'; $book->setstrategy($type); writeln($book->getauthorandtitle());  function writeln($line_in) {   echo $line_in.php_eol; } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -