简体   繁体   English

PHP Composer-php-sql-query-builder

[英]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. 我只是想出了如何安装和使用PHP作曲家,并用它来将php-sql-query-builder安装到我的项目中。 The system created the vendor folder, etc. however I am having issues using classes within the package. 系统创建了vendor文件夹等。但是在使用包中的类时出现了问题。 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 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 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 composer.json

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

Your class source files shouldn't have any require_once statements at all in them. 您的类源文件中根本不应该包含任何require_once语句。 Follow the PSR-4 spec for naming. 请遵循PSR-4规范进行命名。 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. 例如,LoginSystem类应位于名为LoginSystem.php的文件中。

namespace MyNamespace;

class LoginSystem
{
    ...
}

Then set your composer.json to point your namespace to your source directory: 然后设置您的composer.json以将您的命名空间指向您的源目录:

"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();
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM