doupeng8419 2017-04-18 17:30 采纳率: 0%
浏览 48
已采纳

PHP Composer - php-sql-query-builder

I just figured out how to install and use PHP composer and used it to instal php-sql-query-builder to my project. The system created the vendor folder, etc. however I am having issues using classes within the package. It gives me the following error, any suggestions on how I can fix this?

Fatal error: Uncaught Error: Class 'NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder' not found in D:\Documents\CadetPortal\php\lib\login.class.php on line 15

Login.class.php

    require_once ("core.class.php");
    require_once ("../../vendor/autoload.php");

    use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;

    class LoginSystem {
        private $core;
        private $builder;
        private $config;

        function __construct(){
            $this->core = new coreFunctions();
            $this->builder = new GenericBuilder();
            $this->config = require('core.config.php');
        }
//....
    }

EDIT

fncregister.php

require_once "../../vendor/autoload.php";
$LoginManager = new \ThomasSmyth\LoginSystem();

echo $LoginManager->Register($_POST["StrSurname"], $_POST["StrForename"], $_POST["StrEmail"], $_POST["StrPassword"], $_POST["DteDoB"], $_POST["StrGender"], $_POST["StrToken"]);

composer.json

{
    "require": {
        "nilportugues/sql-query-builder": "^1.5"
    },
    "autoload": {
        "psr-4": {
            "ThomasSmyth\\": "php/lib/"
        }
    }
}
  • 写回答

1条回答 默认 最新

  • drxrgundk062317205 2017-04-18 17:56
    关注

    Your class source files shouldn't have any require_once statements at all in them. Follow the PSR-4 spec for naming. Put your classes in a namespace to avoid collision with other classes you might include via composer. Then put one class in one file, named the same as the class. For example, the LoginSystem class should be in a file named LoginSystem.php.

    namespace MyNamespace;
    
    class LoginSystem
    {
        ...
    }
    

    Then set your composer.json to point your namespace to your source directory:

    "autoload": {
        "psr-4": {
            "MyNamespace\\": "src/"
        }
    },
    

    Now, your main app invoker or front controller should be the only place that includes the autoloader:

    require_once 'vendor/autoload.php';
    $login = new \MyNamespace\LoginSystem();
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?