Cannot find libraries added from github in native PHP - php

I'm working on a native PHP project. Using terminal in the root I added 2 github libraries and they get saved in the vendor folder. In a sub folder in the root in a PHP file I add
use nadar\quill\Lexer;
$lexer = new Lexer($row['reply']);
echo $lexer->render();
But I get
Uncaught Error: Class 'nadar\quill\Lexer' not found in /home/USER/public_html/production/filename.php:469
I tried another library and get the same error Cannot find the class. How to solve this? Tis is the first library url: https://github.com/nadar/quill-delta-parser

Every file where you need classes loaded by the autoloader need to include the autoload file. Otherwise, these classes are unknown by the interpreter.
Add this to the beginning of your file:
require __DIR__ . '/vendor/autoload.php';

Related

Include file with his dependencies in PHP

My root project folder is:
c:\phpproject\data_valid.php
In this folder I have a library I need to use:
c:\phpproject\vendor\egulias\email-validator\src\EmailValidator.php
Inside data_valid.php I include the library I need:
//data_valid.php
include ('vendor\egulias\email-validator\src\EmailValidator.php');
The EmailValidator.php has dependencies:
//EmailValidator.php
namespace Egulias\EmailValidator;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Validation\EmailValidation;
When I run the data_valid.php script I get the error:
Fatal error: Uncaught Error: Class "Egulias\EmailValidator\EmailLexer"
So, the data_valid.php file doesn't know where the EmailLexer.php is, being this file called from EmailValidation.php. If I include EmailLexer.php in data_valid.php it is found
//data_valid.php
include ('vendor\egulias\email-validator\src\EmailValidator.php');
include ('vendor\egulias\email-validator\src\EmailLexer.php');
But more dependendencies are missing:
Fatal error: Uncaught Error: Class "Doctrine\Common\Lexer\AbstractLexer
It seems that PHP doesn't include the dependencies of a library automatically.
How do I do that?
PD. After some research I found that including:
include 'vendor/autoload.php';
It loads everything.
But I am still curious how to do it without using include 'vendor/autoload.php'. For example, I have a bunch of classes inside multiple files in a directory. In this scenario I need to call one class but that class depends in multiple classes defined in multiple files in that directory.
Use composer: https://getcomposer.org/
It also allows for manual inclusion of custom classes etc.
I don't recommend manually including all the necessary classes as you would need some recursive search for this anyway.

Why is Imported class not found in PHP file?

I'm attempting to use a class from a repository I installed from packagist with composer. The PHP file I'm writing is under my Project folder, along with the repository which is in Project>vendor.
include "Project/vendor/autoload.php";
include "Project/vendor/smartcat/smartcat-api/src/SmartCAT/API/SmartCAT.php";
use SmartCAT\API\SmartCAT;
use SmartCAT\API\Model\CreateProjectWithFilesModel;
This is at the top of my file and it is how I'm retrieving the smartcat class from the repository. When I try to use the class I wrote $sc= new SmartCAT($login, $password);. This causes the error: PHP Fatal error: Uncaught Error: Class 'SmartCAT\API\SmartCAT' not found in C:\Users\...\Project\createProject.php:20. When I run get_included_files() the necessary files are included. I'm not sure why I'm unable to use the class.
Did you try using the vendor/autoload.php path in the include directive?
If your createProject.php file and vendor folders are located in the same folder, you need to use include "vendor/autoload.php";

Use Composer module in PHP file

I want to use the following AntiXSS library in one PHP file. It is my first time using Composer, but I followed their installation steps and I installed it successfully. I downloaded the library through Composer, updated it and Composer created the vendor/ folder in my directory with the necessary files.
Now I added the following require 'vendor/autoload.php' into my PHP file. I created a new AntiXSS class but I obtain the following error:
Class AntiXSS not found in my directory on line 3.
I tried to use an absolute path instead of vendor/autoload.php but it isn't working yet and I don't know if I should do something more.
Best regards
The class is located in the voku\helper namespace. Use new \voku\helper\AntiXSS() to instantiate it or use use imports to import the namespace.
See php.net for more information about namespaces.

Class not found error when try to use XBase lib in PHP

I am trying to use https://github.com/hisamu/php-xbase lib in my project.
I have copied XBase folder from repository to the my root and created index.php in my root.
When I try this code:
<?php
use XBase\Table;
$table = new Table(dirname(__FILE__) . 'data/test.dbf');
while ($record = $table->nextRecord()) {
echo $record->my_column;
}
?>
I received this error:
Fatal error: Class 'XBase\Table' not found in ...
What is wrong?
You are not requiring the file, that's why PHP can not find the class.
The example you see on GitHub, assumes you have installed and configured composer for your project.
To do this, download and configure composer, then run
composer require 'hisamu/php-xbase: *'
in root folder of your project. Then include vendor/autoload.php in your scripts. All installed classes using composer are now available.
Most frameworks do this for you, so you only use the class as mentioned. But when you are using a framework of yourself, or only plain PHP scripts, it's your responsibility to require autoload.php
Put these lines in the beginning of the table.php file:
include "Column.php";
include "Record.php";

Installing Facebook PHP SDK via Composer w/ Autoload

I have followed the instructions at getcomposer.org combined with those at developers.facebook.com but I am struggling to get the PHP SDK up and running.
The install worked fine and all files were generated including the autoload.php in the vendor folder.
I have put this line in my index.php:
require 'vendor/autoload.php';
When I call FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET'); I am getting a fatal error, CLASS 'FacebookSession' not found.
What do I need to do?
I guess it is something with path names and autoload.php but I can't figure it out.
Vendor folder is at root with index.php.
The use keyword does not include or autoload. It only tells PHP to import the specified namespace to the current scope which has been autoload-ed in this case with composer.
Have you tried to add use Facebook\FacebookSession; (at the beginning of your PHP file after the require vendor/autoload.php line) as xurshid29 commented?

Categories