Note that there are some explanatory texts on larger screens.

plurals
  1. POOverriding PHP Class Instantiation
    primarykey
    data
    text
    <p>I'm currently working on a project where we are developing an application that will be used to power content on multiple sites from a single set of files / hosting environment.</p> <p>There are a number of PHP classes with names such as <code>model_account</code> and <code>model_vehicle</code> which are defined in seperate files named the save as the class name. We are using <code>spl_autoload_register</code> to load the class files as below (simplified):</p> <pre><code>function my_autoloader($class_name) { if(substr($class_name, 0, 6)=='model_') { require_once('models/'.$class_name.'.php'); } } </code></pre> <p>What we want to do, is if the visitor is accessing site xxx and we create an instance of a class, eg <code>$account = new model_account();</code> - actually create an instance of class <code>model_xxx_account</code> if the file exists (<code>models/model_xxx_account.php</code>), but if it doesn't, just include the <code>models/model_account.php</code> file and create an instance of <code>model_account</code>. </p> <p>Likewise, if a visitor came from site yyy, we would want to use <code>model_yyy_account</code> if exists, but full back to <code>model_account</code> if it doesn't.</p> <p>Essentially what we wish to do is create an instance of a different class if conditions are met, but from the same instantiation call.</p> <p>We're not using an off-the-shelf framework like CI or Cake. Also, I've slightly simplified the naming conventions for this post. Sorry if my OO terminology isn't spot on.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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