I have the following setup in a library within CodeIgniter:
class DataModelAutoloader
{
public function __construct()
{
spl_autoload_register(array($this, 'loader'));
log_message('debug', 'QR-Invited autoloader initialized');
}
public function loader($className)
{
log_message("debug", "Accessing loader for class: " . $className);
if (substr($className, 0, 9) == 'datamodel') {
$fullyQualifiedPath = APPPATH.str_replace('\\', DIRECTORY_SEPARATOR, $className).'.php';
log_message('debug', 'Fully qualified path is: ' . $fullyQualifiedPath);
require APPPATH.str_replace('\\', DIRECTORY_SEPARATOR, $className).'.php';
}
}
}
Now, one of my data models, Invite
, in datamodel/Invite.php
is getting loaded, but it's defined as:
class Invite implements JsonSerializable {
...
};
The problem is that it is now trying to load datamodel/JsonSerializable.php
, which, of course, doesn't exist, because I want to use the built-in PHP 5.4.0 JsonSerializable
. (I have PHP 5.5.X installed on the box I'm running on). So, I'm getting the following exception when I run this code:
<p>Severity: Warning</p>
<p>Message: require(qrinvited-application/datamodel/JsonSerializable.php): failed to open stream: No such file or directory</p>
<p>Filename: libraries/Datamodelautoloader.php</p>
<p>Line Number: 20</p>
Is there a way to disable attempted autoloading for things like this that should be built-in to PHP5? Or, perhaps I'm doing something wrong in how I'm extending the class?
If its all about 'namespaces' use implements \JsonSerializable
. The \
makes it so that PHP knows to use the default
namespace instead of the namespace the class currently resides within (in this case, datamodel
).