-1

I'm trying to use PHP SQL Query Builder in a project I'm working on. I am installing it manually. I did this by copying the src directory into my project and then using the following code within my class:

$this->builder = new NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder();

The error I get whenever the builder is used is:

[18-Apr-2017 12:57:48 UTC] PHP Fatal error:  Interface 'NilPortugues\Sql\QueryBuilder\Builder\BuilderInterface' not found in /home/thomassm/public_html/php/lib/sqlbuilder/Builder/GenericBuilder.php on line 24

GenericBuilder.php

namespace NilPortugues\Sql\QueryBuilder\Builder;

use NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory;
use NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface;
use NilPortugues\Sql\QueryBuilder\Manipulation\QueryFactory;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\Column;
use NilPortugues\Sql\QueryBuilder\Syntax\Table;

/**
 * Class Generic.
 */
class GenericBuilder implements BuilderInterface
{//...}

BuilderInterface.php

namespace NilPortugues\Sql\QueryBuilder\Builder;

use NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface;

/**
 * Interface BuilderInterface.
 */
interface BuilderInterface
{
    /**
     * @param QueryInterface $query
     *
     * @return string
     */
    public function write(QueryInterface $query);

    /**
     * @param QueryInterface $query
     *
     * @return string
     */
    public function writeFormatted(QueryInterface $query);
}

I assume the error is somehow caused by the way the files are called, any suggestions?

Thomas Smyth
  • 512
  • 3
  • 9
  • 36
  • from the docs : The recommended way to install the SQL Query Builder is through Composer., why you do install the package manually? – hassan Apr 18 '17 at 13:36
  • 1
    Did you use an autolader ? Your class is not loaded (to be sure, you can check with the php function get_included_files [doc here](http://php.net/manual/fr/function.get-included-files.php). Then you should use the composer autoloader [doc here](https://getcomposer.org/doc/01-basic-usage.md#autoloading). Or you can create and use your own autoloader. – user1344381 Apr 18 '17 at 13:52

1 Answers1

3

I am installing it manually. I did this by copying the src directory into my project and then using the following code within my class: ...

Every source file should be included through include/requrie before using. But PHP allow to setup autoloading for classes, interfaces and traits. In short, when runtime meets an undefined class then a special callback invokes, which can load the relevant file.

Although formally the autoloading rules can be arbitrary, but most of the modern projects supports the common community standard: PSR-4 Autoloader. So, you need an implementation of this standard.

As said in the library description, the recommended way to install is through Composer:

php composer.phar require nilportugues/sql-query-builder

Composer provides own PSR-4 implementation and generates file vendor/autoload.php. Usually this file included at the application entry point. It allow using the classes from all required libraries.

You can use the packages without the Composer (relevant answer). But it requires a lot of work (you still need an PSR-4 autoloader) and this way is rarely used today.

Community
  • 1
  • 1
Timurib
  • 2,735
  • 16
  • 29
  • This may sound a little basic, by can you explain Composers to me. I can't find any good really basic explanation for them (baring in mind I'm going way out of my league using this stuff considering I'm only an A-Level student). What does a Composer actually do? Arrange files in your project directory or set up your web space with those files for you to use? Thanks for your help so far. – Thomas Smyth Apr 18 '17 at 13:58
  • Sorry, but I can hardly explain better than described in the official [introduction](https://getcomposer.org/doc/00-intro.md). – Timurib Apr 18 '17 at 14:02
  • Thanks for your help anyway. I guess I will just have to have a shot at it and hope I get it correct and don't blow something up in the process :) – Thomas Smyth Apr 18 '17 at 14:09