MarketplaceWebService\Client.php 代码
class MarketplaceWebService_Client implements MarketplaceWebService_Interface
{
/** @var string */
private $awsAccessKeyId = null;
/** @var string */
private $awsSecretAccessKey = null;
/** @var array */
private $config = array ('ServiceURL' => null,
'UserAgent' => 'PHP Client Library/2011-08-01 (Language=PHP5)',
'SignatureVersion' => 2,
'SignatureMethod' => 'HmacSHA256',
'ProxyHost' => null,
'ProxyPort' => -1,
'MaxErrorRetry' => 3,
);
}
文件名与类名不一样,类名就是路径,以_为分隔符。这路情况下如何写autoload规则。假设我的目录结构如下:
amazon
- src
- - MarketplaceWebService
- - - Client.php
- - - Model
- - - - - GetReportResult.php
- .gitignore
- composer.json
- README.md
Autoload应该如何定义,以及如何调用MarketplaceWebService_Client与MarketplaceWebService_Model_GetReportResult两个类?
不懂你的文件名与类名不一样是什么意思,不过我的建议是按照 psr0 和 psr4 的规范来组织和命名代码,详见 composer 有关 autoload 的文档。
只要你的类 按照PSR-0标准组织起来,就可以使用 composer
的autoload
。不知道你的 namespace
有没有包含 src
。
这是 PSR-0的 autoload示例代码,看下应该就明白了:
<?php
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
PSR-0 的标准: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
composer发包: https://packagist.org