Fatal error: Class __ not found - php

Last spring I was working on a project using PHP, and it was working well. I had no time to work on it later, but now after I changed hosting I want to work on it again. I send files in working directory, but the error appears. It says that can't find Class TemplateLogin in class Login
Login.php:
class Login extends TemplateLogin
TemplateLogin.php:
abstract class TemplateLogin extends Core
Login.php and TemplateLogin.php are in the same folder

Your file Login.php needs to require or include the file TemplateLogin.php:
require_once 'TemplateLogin.php';
Or php will not know how the class TemplateLogin is defined.
An alternative to manually including/requiring every file is to define an autoloading function.

Related

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

Cannot use a custom PHP plugin in Laravel

I have installed a plug-in called tet. I copied tet.so in the plug in folder and included it in php.ini
I have checked phpinfo and says that the plugin is enabled and running.
I have created a php page to test it.
<?php
new tet();
?>
And works perfectly.
However when I try this on my laravel it gives me this error:
FatalThrowableError in mycontroller.php line 16: Class
'App\Http\Controllers\tet' not found
tet is not a controller. Tet is a class created by the php plugin. How do I include this in laravel?
You should try
new \tet();
because you are using it inside a namespace. Inside a namespace any reference to a class is considered as relative to the namespace you are in. With the "\" you can "escape" the namespace and so you can access classes outside.

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

Drupal 8 autoload don't load custom class

I recently decided to develop a new website with drupal 8 (i never used drupal 7 or other version).
I had to create a module and i need a custom class in this module and i create a service to use it. But i've an error, my custom class is not find.
When i had a require of my class in the autoload.php, it's work. So my service is correct but my class is not include in my autoload.
Anyone have a solution for my problem ?
Do you "use" the class? Like that:
use Drupal\your_namespace\yourClass;
Of course your class needs a namespace to make this work.
you need follow PSR4 rule, if your class is defined in module, you should put your class file into the correct folder structure , for example if your class namespace defined as "Drupal\your_module_name\yourClass" , the file folder structure should be: modules\your module name\src\ (your Class file)

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 :-)

Categories