How to call the right object classes based on variable in PHP? -


situation

using php 5.5,

i allow user submit form has dropdown.

based on dropdown, call different classes particular operation.

what's wrong?

currently, use switch case, number of classes balloon 3 25. switch case ugly.

this code

    // call right reader based on vendor     switch($vendor) {         case 'a':             $vendorreader = new areader();             break;         case 'b':             $vendorreader = new breader();             break;         case 'c':         default:             $vendorreader = new creader();             break;     } 

i deliberately named these classes way, easier use variable $vendor call constructor of relevant $vendorreader class.

what's way call right reader class based on variable?

if user selection coming dropdown, can infer it's coming form via get/post? in case, can not use name of menu?

if (isset($_post['my_class_selection_dropdown)) {      $selection = $_post['my_class_selection_dropdown];      if(class_exists($selection)) {          $vendorreader = new $selection();      }    } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -