Reputation: 89
I've tried to use the AWS SDK according to this: http://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html
I've tried both the .phar-file and unpacking the zip but I run into issues.
I use the __autoload function in PHP like this:
// autoload classes
function __autoload($class) {
require_once ('./system/classes/'.strtolower($class).'.php');
}
This works fine by itself. However, I include the SDK like so:
require '/path/to/aws.phar';
My system cannot find my own classes any more (the ones that hasn't been called yet at the time I include AWS SDK that is).
What am I missing? What have I done wrong?
Upvotes: 2
Views: 778
Reputation: 7564
It is not to difficult to learn the PHP-FIG SP4 autoload standard and do it yourself. This uses spl_autoload_register()
and you can use more than one autoloader. Here is an example of a do-it-yourself autoloader class I made just reading the PHP-FIG standards and the PHP manual.
<?php
namespace Acme\Framework; //Just to use namespaces in this example.
class Autoloader
{
private function __construct()
{
;
}
private function __clone()
{
;
}
private static function autoload($qualifiedClassName)
{
$nsPrefix = 'Acme\\';
$baseDir = 'C:/public/www/acme/src/'; // /public/www/acme/src/
$nsPrefixLength = strlen($nsPrefix);
if (strncmp($nsPrefix, $qualifiedClassName, $nsPrefixLength) !== 0) {
return; //Go to next registered autoloader function / method.
}
$file = $baseDir . str_replace('\\', '/', substr($qualifiedClassName, $nsPrefixLength)) . '.php'; //substr() returns the string after $nsPrefix.
if (!file_exists($file)){
echo "<h1>$file</h1>";
throw new \RuntimeException("The file {$file} does not exist!");
}
if (!is_file($file)){
throw new \RuntimeException("The file {$file} is not a regular file!");
}
if (!is_readable($file)){
throw new \RuntimeException("The file {$file} is not readable!");
}
require $file;
}
public static function init()
{
/*
Just make another method in this class and alter this code
to run spl_autoload_register() twice.
*/
if(!spl_autoload_register(['self', 'autoload']))
{
throw new \RuntimeException('Autoloader failed to initialize spl_autoload_register().');
}
}
}
I use it like this during bootstrap time.
require 'Autoloader.php'; //Autoloader for objects.
Autoloader::init();
This could be altered to support another autoloader for code in a different directory.
I hope this was helpful. Good luck to you, and may your project be a success!
Sincerely,
Anthony Rutledge
Upvotes: 1
Reputation: 1633
This is because using the __autoload
method you can only have one autoloader, and aws needs to add its own autloader. It is much better to use spl_autoload_register
, as this allows for multiple autoload functions, so yours will still be available even when aws.phar has added it's own.
Try this:
spl_autoload_register(function ($class) {
require_once ('./system/classes/'.strtolower($class).'.php');
});
See docs on this here: http://php.net/manual/en/function.spl-autoload-register.php
Upvotes: 2