我使用Pomm 1.1.4,无法加载该类。这里it says Fatal error: Class 'Pomm'Connection'Database' not found in /users/ilhanna/public_html/api/v1/Pomm-1.1.4/Pomm/Service.php on line 38
第38行没有任何内容,只有一条注释。我加载类的代码是
require_once 'Pomm-1.1.4/Pomm/Service.php';
# Using the constructor
$service = new Pomm'Service(array(
'db_one' => array(
'dsn' => 'pgsql://username:password@localhost:5432/databasename'
)
));
我想我错过了什么。
我建议您使用composer安装Pomm并获得自动加载器。从packagist 下载composer.phar
创建一个composer.json文件,如下所示:
{
"require": {
"pomm/pomm": "~1.1"
}
}
并执行phar来安装Pomm。只需在index.php 中添加以下代码
$loader = require __DIR__."/vendors/autoload.php";
如果您不能使用composer,请创建自己的自动加载机制:
spl_autoload_register(function ($class) {
if (0 === strpos($class, 'Pomm''')) {
$class = str_replace('''', '/', $class);
require sprintf("%s/%s.php", __DIR__, $class);
}
在index.php文件一开始,它应该运行良好。
您的Service.php
文件包括类Service
,该类具有另外两个类依赖关系。该错误适用于缺少的Database
类。
namespace Pomm;
use Pomm'Connection'Database; // Error line (class missing)
use Pomm'Exception'Exception;
在创建服务实例之前,还需要加载这些类。如果文件与服务类位于同一目录中;你可以先把它们包括进来。
require_once 'Pomm-1.1.4/Pomm/Datebase.php';
require_once 'Pomm-1.1.4/Pomm/Exception.php';
require_once 'Pomm-1.1.4/Pomm/Service.php';
$service = new Pomm'Service(array(
'db_one' => array(
'dsn' => 'pgsql://username:password@localhost:5432/databasename'
)
));
一个更好的解决方案是研究PHP自动加载;如果你的项目比较大的话,也许是像Composer这样的包管理器。