I’m writing API implementation and my main API class has __call() magic method:
public function __call($name, $params)
{
if (in_array($name, $this->resources))
{
require_once APPPATH . 'resources' . DIRECTORY_SEPARATOR . $name . '.php';
$class_name = ucfirst($name);
return new $class_name($params);
}
}
So basically in my application if I write
$api->product()->get($product_id);
// or
$api->product()->post($product);
resources/product.php file is included, Product object created and appropriate method called. Is this a correct way to do lazy loading and is there a better way to implement an API?
You can add your own autoloader afterwards. If “first” autoloader does not find file you have maybe a second logic for it or a third…
spl_autoload_register(); @http://www.php.net/manual/en/function.spl-autoload-register.php
In that situation you dont have to care about your frameworks/applications autoloader.