Note that there are some explanatory texts on larger screens.

plurals
  1. POphp classes - how would one go about loading a "module" object within an existing object only when its needed?
    primarykey
    data
    text
    <p>Ok so i have the following code setup which seems to work fine:</p> <p>user handler "module":</p> <pre><code>class user_handler { private $dbo; public function __construct($dbo) { $this-&gt;dbo = $dbo; } public function user_table_method() { $this-&gt;dbo-&gt;generic_db_method(); } } </code></pre> <p>the connection class:</p> <pre><code>class connection { private $dbc; public $user; public function __construct() { $this-&gt;dbc = 'connection'; $this-&gt;user = new user_handler($this); } public function generic_db_method() { echo '&lt;p&gt;doing stuff with ' . $this-&gt;dbc . '&lt;/p&gt;'; } } </code></pre> <p>Then i can access user handler methods like so:</p> <pre><code>$dbc = new connection(); $dbc-&gt;user-&gt;user_table_method(); </code></pre> <p><strong>My question is this:</strong></p> <p>Would it be possible with a method in the <code>connection</code> class to create the <code>_user</code> object only when it's required?</p> <p>What i'm thinking of is a method that would be used like so:</p> <pre><code>$dbc-&gt;add_handle('user', 'user_handler'); </code></pre> <p>Which would create a new public property called <code>user</code> from scratch and set it as a new <code>user_handler</code> object:</p> <pre><code>// in more detail public function add_handle($name, $module) { if(!isset($this-&gt;/*somehow use $name*/)) { $this-&gt;/*somehow use $name*/ = new /*somehow use $module*/(); } } </code></pre> <p>..so the new way to set it up would be like:</p> <pre><code>$dbc = new connection(); $dbc-&gt;add_handle('user', 'user_handler'); $dbc-&gt;user-&gt;user_table_method(); </code></pre> <p>Thanks in advance! (looking at php 5.2 and up)</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