Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP自动加载自己的类和AWS SDK_Php_Amazon Web Services - Fatal编程技术网

PHP自动加载自己的类和AWS SDK

PHP自动加载自己的类和AWS SDK,php,amazon-web-services,Php,Amazon Web Services,我尝试使用AWS SDK的原因如下: 我尝试了.phar文件和解压缩zip文件,但遇到了问题 我在PHP中使用_autoload函数,如下所示: // autoload classes function __autoload($class) { require_once ('./system/classes/'.strtolower($class).'.php'); } 这本身就行。但是,我包括SDK,如下所示: require '/path/to/aws.phar'; 我的系统再

我尝试使用AWS SDK的原因如下:

我尝试了.phar文件和解压缩zip文件,但遇到了问题

我在PHP中使用_autoload函数,如下所示:

// autoload classes
function __autoload($class) {
    require_once ('./system/classes/'.strtolower($class).'.php');
}
这本身就行。但是,我包括SDK,如下所示:

require '/path/to/aws.phar';
我的系统再也找不到我自己的类(在我包含AWS SDK时还没有调用的类)


我错过了什么?我做错了什么?

这是因为使用
\uu autoload
方法只能有一个自动加载器,aws需要添加自己的自动加载器。最好使用
spl\u autoload\u register
,因为这允许多个自动加载功能,所以即使aws.phar添加了自己的功能,您的功能仍然可用

试试这个:

spl_autoload_register(function ($class) {
    require_once ('./system/classes/'.strtolower($class).'.php');
});
请参见此处的文档:
学习标准并自己动手并不难。这将使用并且您可以使用多个自动加载器。下面是我在阅读PHP-FIG标准和PHP手册时创建的一个自己动手的自动加载器类的示例

<?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().');
        }
    }
}
可以对其进行更改,以支持另一个自动加载程序在不同目录中加载代码

我希望这是有帮助的。祝你好运,祝你的项目成功

真诚地


安东尼·拉特利奇(Anthony Rutledge)

使用作曲家的作品,这是迄今为止最简单的。使用最适合你的作品。我想我还有很多东西要学。非常感谢,我要试试这个!我还建议您看看Composer,它不仅可以管理php中其他库的依赖项,还可以让您轻松地配置自己的类以自动加载。
require 'Autoloader.php';   //Autoloader for objects.
Autoloader::init();