Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP autoloading helpers inside a class
    text
    copied!<p>I currently have a manual method for registering helpers into my base connection class which goes pretty much as follows:</p> <pre><code>class db_con { // define the usual suspect properties.. public $helpers; // helper objects will get registered here.. public function __construct() { // fire up the connection or die trying $this-&gt;helpers = (object) array(); } public function __destruct() { $this-&gt;helpers = null; $this-&gt;connection = null; } // $name = desired handle for the helper // $helper = name of class to be registered public function register_helper($name, $helper) { if(!isset($this-&gt;helpers-&gt;$name, $helper)) { // tack on a helper.. $this-&gt;helpers-&gt;$name = new $helper($this); } } // generic DB interaction methods follow.. } </code></pre> <p>Then a helper class such as..</p> <pre><code>class user_auth { public function __construct($connection){ } public function __destruct(){ } public function user_method($somevars) { // do something with user details } } </code></pre> <p>So after creating the <code>$connection</code> object, i would then manually register a helper like so:</p> <pre><code>$connection-&gt;register_helper('users', 'user_auth'); </code></pre> <p>Now my question is, could I somehow autoload helper classes inside the base connection class? (within the <code>register_helper()</code> method or similar) Or am I limited to loading them manually or via an external autoloader of some form?</p> <p>My apologies if this question has been answered elsewhere, but I just haven't found it (not for lack of trying) and I haven't any real experience autoloading anything yet.</p> <p>Any help or pointers greatly appreciated, thanks in advance! :)</p> <p><strong>EDIT:</strong> As per Vic's suggestion this is the working solution I came up with for the register method..</p> <pre><code>public function register_handlers() { $handler_dir = 'path/to/database/handlers/'; foreach (glob($handler_dir . '*.class.php') as $handler_file) { $handler_bits = explode('.', basename($handler_file)); $handler = $handler_bits[0]; if(!class_exists($handler, false)) { include_once $handler_file; if(!isset($this-&gt;handle-&gt;$handler, $handler)) { $this-&gt;handle-&gt;$handler = new $handler($this); } } } } </code></pre> <p>This appears to include and register the objects absolutely fine for now, whether this solution is a "good" one or not, I can't know without more input or testing.</p>
 

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