Class not found, composer and Zend Framework 1 autoloader issue - php

I have the following class autoload definition in a [root]/composer.json file:
{
...
"autoload": {
"psr-0": {
"": [
"application/models",
"application/controllers",
"application/forms",
"library/"
]
},
"psr-4": {
"": ["src/"]
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
...
}
When I call the [root]/public_html/index.php page I got the following error:
PHP Fatal error: Uncaught Error: Class 'classes\DependencyInjection' not found in /var/www/html/application/bootstrap.php:29
What's in [root]/public_html/index.php is the following code:
$bootstrap = true;
require_once '../application/bootstrap.php';
And what's in [root]/application/bootstrap.php file is:
// turn on autoloading for classes
// composer autoloader
include(MMIPATH.'/vendor/autoload.php');
// zend autoload
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
$diContainer = new classes\DependencyInjection(services.yaml');
$proxy = $diContainer->get('containerProxy');
This is the definition of [root]/library/classes/DependencyInjection.php:
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
USE Symfony\Component\DependencyInjection\Container;
class DependencyInjection extends ContainerBuilder
{
....
}
What's wrong here? Why autoloader can't find that class?

You're attempting to load a "classes" namespace, however your class is not defined as being in the "classes" namespace.
new classes\DependencyInjection(...) in PSR-0 loads {paths}\classes\DependencyInjection.php and attempts to instantiate the class DependencyInjection from the namespace classes, but DependencyInjection is not in the classes namespace. The file will load but the class does not exist.
You could add namespace classes; to each of these classes, but that's not exactly a good solution. Better solution is to use proper namespacing or change your PSR-0 list to include library/classes and use new DependencyInjection(...). (My vote is for the first one -- use proper namespaces.)
As requested. example:
File Location
{app}\library\UsefullNamespace\DependencyInjection.php
Call it using
new UsefullNamespace\DependencyInjection.php
DependencyInjection.php:
namespace UsefullNamespace;
use [...];
class DependencyInjection extends ContainerBuilder
{

Related

Problem with PSR-4 Autoloader using composer

I have this directories structure:
src
- PrintXMLInfo.php
- PrintListInfo.php
- PrintInformation.php
vendor
main.php
composer.json
Now in composer.json I declared this psr-4 autoload:
"autoload": {
"psr-4": {
"TemplateMethod\\": "src/"
}
}
And in the abstract class PrintInformation I put:
namespace TemplateMethod;
abstract class PrintInformation
And Extended it in PrintXMLInfo and PrintListInfo
namespace TemplateMethod;
use TemplateMethod\PrintInformation;
class PrintXMLInfo extends PrintInformation
The same in PrintListInfo.
The problem is "use TemplateMethod\PrintInformation";
I'm in the same namespace of PrintInformation, but if I write:
use PrintInformation;
i get an error of class not found:
PHP Fatal error: Class 'PrintInformation' not found in /home/vagrant/shared/template_method/src/PrintXMLInfo.php on line 6
I can't get where I'm wrong, if parent and child class are in the same namespace shouldn't I be able to call them without namespace prefix in use, right?

How to add custom class in laravel 5.4.10

I am new in laravel.
I am writing following code in the controller method.
include_once(app_path() .'/Classes/Pgp_2fa.php');
$pgp = new Pgp_2fa();
Following errors are shown.
FatalThrowableError in AuthController.php line 146: Class
'App\Http\Controllers\Pgp_2fa' not found
You have to use the correct namespace for your class.
If you are not using any namespace, you should add a \ in front of the class to let php know that the class exists in the root namespace. Otherwise php will look in the current namespace which is App\Http\Controllers when you are in a controller.
$pgp = new \Pgp_2fa();
This is not just Laravel behavior but php in general.
When you add new classes there are couple way to use them. (I mean, load them)
If your classes has unique name and don't use namespace, then you can add backslash \ start of the class.
$class = new \Pgp_2fa;
But if you want to define them namespaces and use with same name so
many times, then you have to add these classes in composer.json. Open your
composer.json and come into "autoload" section and define class
directory in classmap
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Classes", // so all classes in this directory are going to load
],
And don't forget to say composer dump-autoload after these changes and creating new classes
If you don't want to dump your composer when you add a new class,
then you may want add those classes inside of the psr-4 section in composer
"autoload": {
"psr-4": {
"App\\": "app/",
"App\\Classes\\": "app/Classes",
}
},
you need to add namespace for your custom class
or just add slash on your class name. like :
$pgp = new \Pgp_2fa();
You can call inside the constructor like below, and you can use the
$Pgp_2fa variable in remaining function
class controllername extends Controller
{
public function __construct(Route $route,Request $request)
{
$Pgp_2fa = new \Pgp_2fa();
}
}
You can easily add your custom classes by added it to Composer's autoload array. Go to composer.json and add it to:
"autoload": {
"files": [
"app\\Classes\\Pgp_2fa.php"
]
},
Now in your app folder, under classes folder add your Pgp_2fa.php file which will have your class. You can use it anywhere you want, it should be automatically auto-load-ed.
After adding your class write command:
composer dumpautoload to refresh autoload class mapping.
For example, There is our class Common.php in a directory called Mylibs in app directory.
app/mylibs/Common.php
we need to namespace App\Mylibs;
namespace App\Mylibs;
class Common {
public function getSite() {
return 'AmirHome.com';
}
}
Now, you can access this class using the namespace within your classes just like other Laravel classes.
use App\Mylibs\Common
in Controller:
namespace App\Http\Controllers;
use App\Mylibs\Common;
class TestController extends Controller {
protected $common;
public function __construct(Common $common) {
$this->common = $common;
}
public function index() {
echo $this->common->getSite();
}
}

Composer psr-4 autoload issue

I have problem with autoloading with composer when i use psr-4 autoloading it doesn't work and give me error.
I tried:
$ composer dump-autoload
and a lot of other thing but it doesn't work without
require one;
error:
You are now a master builder, that knows how to autoload with a
classmap!
Fatal error: Uncaught Error: Class 'VegithemesLibraryGreeting' not
found in /home/vaclav/Server/vssk/VSSK/project/aldemo/index.php:10
Stack trace: #0 {main} thrown in
/home/vaclav/Server/vssk/VSSK/project/aldemo/index.php on line 10
composer.json:
{
"autoload": {
"files": ["mylibrary/functions.php"],
"classmap": [
"classmap"
],
"psr-4": {
"one\\": "src/"
}
}
}
greeting.php (file with class to load):
<?php
namespace one;
Class Greeting
{
public function hi()
{
return "We got you covered";
}
}
index.php file:
<?php
require 'vendor/autoload.php';
echo lego();
$cm = new Cmautoload;
echo $cm->classmap();
$vt = new oneGreeting;
echo $vt->hi();
It is generally good practice to capitalize the first letter of a class name. It also adheres with the rules of PSR-1.
Change your composer.json file to look like this:
{
"autoload": {
"files": [
"mylibrary/functions.php"
],
"classmap": [
"classmap"
],
"psr-4": {
"One\\": "src/"
}
}
}
Now, in your index file. We are going to import the autoloader. To do this simply require it:
require 'vendor/autoload.php';
Now that you have included the autoloader, go into every class and set the namespace.
The classes in your src/ == namespace One;
Check your classes in src/ and make sure they are all namespaced. Meaning that they should all have the following line of code at the top:
namespace One;
As mentioned before, update your file names to Foo.php and class names to
class Foo to adhere to PSR. (This is not required but highly recommended and standard procedure.)
To use one of your classes you would say use One\Greeting;
$greeting = new Greeting();
echo $greeting->hi(); //"We got you covered"
I found the problem, there was missing:
use One\Greeting;
In a lot of tutorials there isn't a word about it.
Another relevant detail about this is the namespace must match with the folder structure.
If not it will throw the warning.
In my case the filename was
src/One/GreetingClass.php
but the class name was in lowercase, causing this error:
class Greetingclass {
Changing the class declaration as GreetingClass fixed the issue.

Class not found issue (Composer, PHPUnit)

Getting the following error form PHPUnit:
Fatal error: Class 'FoobarTest\Money\Money'
not found in /www/foobar/tests/FoobarTest/Money/MoneyTest.php on line 11
My structure is like:
/src/Foobar/Money/Money.php (class Money, namespace Foobar\Money)
/tests/FoobarTest/Money/Money.php (class Money, namespace FoobarTest\Money)
Autoloading done through composer:
"autoload": {
"psr-4": {
"Foobar\\": "src/"
},
"psr-0": {
"FoobarTest\\": "tests/"
}
},
Tried with PSR0, PSR2, PSR4, ...
MoneyTest class:
<?php
namespace FoobarTest\Money;
class MoneyTest extends \PHPUnit_Framework_TestCase
{
// ...
Money class:
<?php
namespace Foobar\Money;
class Money
{
// ...
Why is it trying to load FoobarTest\Money\Money instead of Foobar\Money\Money ?
To help php autoloader (and composer) you must import the target class using
use Foobar\Money\Money;
in your test file.
Also you probably want to give your test file a MoneyTest.php name to match the corresponding class name.

How to use __autoload function when use composer load

app/Core
contollers
This is my website structure to put the main class, I user composer psr-4 rule to import class under app/Core folder.
composer.json
{
"autoload": {
"psr-4": {
"Core\\": ["app/Core"]
}
}
}
index.php
<?php
include 'vendor/autoload.php';
new Core/Api; // it's work
It works fine, but I want to autoload class under controllers folder without use namespace, so I use __autoload function like:
index.php
<?php
include 'vendor/autoload.php';
function __autoload($class_name) {
include 'controllers/' . $class_name . '.php';
}
new Test // Fatal error: Class 'test'
If I remove include 'vendor/autoload.php'; it will work, so I think the code is correct, I know I can use classmap in composer.json, but it need to dump-autoload everytime I add a new class, how to deal with the conflict?
You do not have to use your own implementation of autoloading. You could use composer autoloading for all classes.
{
"autoload": {
"psr-0": { "": "src/" }
}
}
https://getcomposer.org/doc/04-schema.md#psr-0
Or, you could create class map
https://getcomposer.org/doc/04-schema.md#classmap
p.s. indeed, you could use empty namespace with psr-4.

Categories