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 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?