doupeng8419 2017-04-18 17:30
浏览 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();
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵