Include file with his dependencies in PHP - 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.

Related

Cannot find libraries added from github in native 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';

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";

PHP: spl_autoload_register attempting to load files from wrong directory

I am trying to implement autoloading of my classes. I separate my classes into two different folders:
/classes
/external
In my external folder, I keep everything third-party that I am using within my application. It may include classes, functions, etc. I like to keep that separate from my own code.
I want to include everything in the external directory manually. I only want files in the classes directory to be autoloaded. Here is what I have:
Files:
/classes/cache.class.php
/classes/db.class.php
/classes.mail.class.php
/external/hooks.php
And to autoload:
spl_autoload_register(function ($class_name) {
include dirname(__FILE__).'/classes/'.$class_name.'.class.php';
});
$Cache = new cache();
$Db = new db();
$Mail = new mail();
Somehow, this is attempting to load any class that exists in the external directory. I know this because in hooks.php I have:
class Hooks { ... } // Notice the capital H
And I am getting the error:
"Warning: include(/path/to/classes/Hooks.class.php): failed to open stream: No such file or directory"
How can I get spl_autoload_register to ignore all classes that exist outside of the classes directory?
The autoloader has no way of knowing in which folder a class is defined. The general flow is:
See an undefined class being used in the code being executed
Call all the functions registered with the spl_autoloader, and pass them the undefined class name
Since you didn't have any check for if the file exists, the autoloader function you registered is erroring out whenever you fail to include a non-autoloaded class before it's used. As #Forbs mentioned in his answer, just add a check in your autoloader function to see if the file exists before including the source file.
It's worth noting that Composer does this job for you very well already - unless you have some specific reason for wanting to use your own autoloader, I'd suggest just setting up composer for your project.
https://getcomposer.org/doc/00-intro.md
add this
if (file_exists(dirname(__FILE__).'/classes/.$class_name.'.class.php'))
right before your include

How to include external PHP classes in Joomla

I have few PHP classes and files that I want to include in Joomla so that I can call those classes. I tried doing require_once config.php to include a PHP file which also includes all the classes that I would want to use in Joomla.
But each time I execute the page I receive the error below:
Fatal error: Class 'SomeClassName' not found
is there any other way to include external PHP classes or files in Joomla?
Thanks in advance!
Please use Joomla autoloader. Is better.
<?php
// Register an adhoc class.
JLoader::register('AdhocClass', '/the/path/adhoc.php');
// Register a custom class to override as core class.
// This must be done before the core class is loaded.
JLoader::register('JDatabase', '/custom/path/database_driver.php', true);
Edit:
Load classes with autoload instead of require/include have a better performance, because PHP will only read (require access to the disk) and compile (require memory and CPU usage) if you really use your class.
To do the same with require/include you have to be sure to only use if will really use the class.
Source:
http://developer.joomla.org/manual/ch01s04.html
require_once should work just fine within Joomla. Make sure the class you want to use is really loaded within your file, and the file is properly referenced in the require_once. Something is going wrong there and it has nothing to do with Joomla itself :-)

Fatal error: Call to undefined method Jobeet::slugify()

I have own project and i would like add for this class same as in Jobeet tutorial:
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/05
I copied exactly same as in this article, but i have error:
Fatal error: Call to undefined method Jobeet::slugify()
Any own class for /lib is not included in my actions, but methods getTable, from Doctrine etc working. Not working only my class for all folder /lib. Why? How can i enable this?
First, do you have this file: Jobeet.class.php ?
Second, this is strange because by default, classes stored in the following directories in your projects benefit from the autoloading automatically:
myproject/lib/
myproject/lib/model
myproject/apps/frontend/lib/
myproject/apps/frontend/modules/mymodule/lib
You can force the autoload when you need to load lib from other folders. Check the doc for that (this is for sf1.2 but it's work for 1.4).

Categories