Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It sounds like you want to use the <a href="http://en.wikipedia.org/wiki/Factory_method_pattern#PHP" rel="nofollow noreferrer">Factory design pattern</a></p> <p>Make this a library:</p> <pre><code>class MyModelFactory { static public function Factory($data) { $type = key($data); return new $type($data); } } </code></pre> <p>now, in your controller, you can do something like this:</p> <pre><code>$model = MyModelFactory::Factory(array($_REQUEST['model'] =&gt; $_REQUEST)); </code></pre> <p>and now you have an object of whatever model was specified in $_REQUEST['model']. Be sure to take any security precautions you may need for your application to assure the user has permissions to use the model that they request</p> <p>Now, since you want to be using common methods and stuff, your models should probably be based off an abstract class / interface.. so instead of </p> <pre><code>class MyModelOne extends Model { // stuff } </code></pre> <p>You probably want something like this, to ensure your required methods will always be available:</p> <pre><code>abstract class MyAbstractModel extends Model { protected $search_params; public function __construct($data = array()) { $search_params = $data['search_params']; } protected function GetSearchParameters() { return $this-&gt;search_params; } abstract public function GetData(); abstract public function GetColumns(); abstract public function DefineViewOptions(); } class MyModelOne extends MyAbstractModel { public function GetData() { $params = array(); $params[] = $this-&gt;db-&gt;escape_str($this-&gt;GetSearchParameters()); // return whatever data you want, given the search parameter(s) } public function GetColumns() { // return some columns } public function DefineViewOptions() { // return some configuration options } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload