PHP自定义命名空间不起作用

时间:2016-05-18 03:02:45

标签: php namespaces composer-php

我刚刚通过Xampp在本地建立了一个网站。一切正常,它使用PHP 5.6版。我使用composer来使用一些第三方应用程序,例如Guzzle和Stringy。完成后我上传到我的Godaddy虚拟主机帐户,该帐户使用PHP 5.5。当我加载网站时,我收到此错误:

致命错误:第81行的public_html / portal / conf / settings.php中找不到类'Libs \ Model \ Site_Settings'

然而,供应商名称空间工作得很好。我没有收到任何错误。哎呀我的自定义类没有任何错误。我正在使用作曲家自动加载所有内容。一切都在本地完美运行,我使用自定义命名空间的任何类都不能只在我的网络托管帐户上工作。在我的课程中,我在顶部:

namespace Libs\Model;

我也尝试过使用括号

namespace Libs\Model {\\code here}

尝试研究这个问题,一无所获。有什么建议?在psr4自动加载文件中显示:

'Libs\\' => array($baseDir . '/lib')

我验证了$ baseDir指向正确的文件夹。

更新

以下是我试图打电话的课程的代码。很简单:

namespace Libs\Model;

class Site_Settings {

    private $dbconn;

    public function __construct($dbconn)
    {

        $this->dbconn = $dbconn;            

    }

    public function findSiteSettings($domain)
    {

        //We clean any variables being passed to the query
        $domain = $this->dbconn->escape($domain);

        //We turn on query caching
        $this->dbconn->cache_queries = TRUE;

        //This is the query statement to run
        $query = $this->dbconn->get_row("
                SELECT
                    jp.*,
                    js.stateabb,
                    js.statename,
                    js.statecountry
                FROM
                    job_site AS jp
                INNER JOIN
                    job_state AS js
                ON
                    jp.stateid = js.id
                WHERE
                    jp.sitedomain = '$domain'
                AND
                    jp.active = 1
                LIMIT
                    1
                ");

        //We turn off query caching
        $this->dbconn->cache_queries = FALSE;

        //We now return any rows found
        return $query;

    }

}

这就是我所说的:

//We include the autoloader that is needed to load all vendors for this site
    include(VENDORS .'autoload.php');

    //We get the site settings for this job site
    $settings = new Libs\Model\Site_Settings($global_db);
    $site_settings = $settings->findSiteSettings($global_sitedomain);

这是我作曲家psr4的自动加载文件:

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname(dirname($vendorDir));

return array(
    'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'),
    'Stringy\\' => array($vendorDir . '/danielstjules/stringy/src'),
    'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src'),
    'Libs\\' => array($baseDir . '/lib'),
    'League\\Plates\\' => array($vendorDir . '/league/plates/src'),
    'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'),
    'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'),
    'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
    'Cocur\\Slugify\\' => array($vendorDir . '/cocur/slugify/src'),
);

更新#2 这是我的作曲家文件

    "autoload": {
        "classmap": [
            "lib/vendor/ezsql/mysqli/ez_sql_mysqli.php",
            "lib/vendor/ezsql/shared/ez_sql_core.php",
            "lib/helper/url.php",
            "lib/helper/html.php",
            "lib/helper/form_message.php",
            "lib/helper/email_generator.php",
            "lib/helper/pagination.php"
        ],
        "psr-4": {"Libs\\": "lib"}
    },
    "require": {
        "league/plates": "^3.1",
        "guzzlehttp/guzzle": "^6.2",
        "phpmailer/phpmailer": "^5.2",
        "cocur/slugify": "^2.1",
        "danielstjules/stringy": "^2.3",
        "wixel/gump": "^1.3",
        "jwage/purl": "^0.0.7"
    }
}

1 个答案:

答案 0 :(得分:0)

您还没有提供足够的信息来真正回答您的问题。 php文件位于Libs \ Model \ Site_Settings的哪个位置?您是否正在使用像OSX这样的不区分大小写的系统并上传到像Linux这样的区分大小写的系统?你使用什么自动加载标准?看起来你正在使用PSR-0和PSR-4。

您可能需要为composer.json添加自动加载路径:

    "autoload": {
        "psr-4": {
            "Libs\\Model\\": "lib/src/model",