Creating a New Instance of a Subclass from within a Baseclass
<p>I want to be able to first create a new instance of a base class, then from within that base class I want to be able to create a new instance of a subclass.  I also want to be able to call base class methods from within the subclass.  I tried it with the code below and it works how I want, but is this a bad practice and is it memory efficient?  Any other comments?</p>

<pre><code>&lt;?php
class BaseClass {
    private $subclass = NULL;
   function __construct() {
       print "In BaseClass constructor&lt;br&gt;";
       $this-&gt;subclass = new SubClass();
   }
   function sayhello() {
    print 'hello from base class';
   }
}

class SubClass extends BaseClass {
   function __construct() {
       print "In SubClass constructor&lt;br&gt;";
       $this-&gt;sayhello();
   }

   function saygoodbye($num) {
    print 'goodbye from base class';
   }
}

$SUB = new SubClass();
?&gt;
</code></pre>

<p>My intention of doing this was to be able to access everything by using <code>$this-&gt;</code>, like how CodeIgniter works.  This is what I have in the index.php file for my private API.  I want to be able to authenticate the API request before calling call_user_func_array().  I also need to connect with a database for the authentication, but I want to be able to access the DB from the subclass as well.  Essentially, the code figures out what API method is being called, then creates an instance of the subclass and finally calls the method.</p>

<pre><code>$get_contents = $_GET;

$resource_name = $get_contents['_resource'];
unset($_GET['_resource']);

$resource_method = $get_contents['_resource_method'];
unset($_GET['_resource_method']);

//Load the resource class
include 'core/resources/' . $resource_name . '.php';

//If set to true, each method will require an API key to be used
$auth_required = array();
$auth_required['collections']['addcollection'] = TRUE;

if(isset($auth_required[$resource_name][$resource_method]) &amp;&amp; $auth_required[$resource_name][$resource_method] == TRUE) {
    print 'requires auth';
}

//Create a new instance of the resource
$API = new $resource_name();

$method_params = $_GET;

call_user_func_array(array(&amp;$API, $resource_method), $method_params);
</code></pre>
