Composer psr-4 autoload issue - php

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.

Related

Composer - Class Not Found

I'm still having problems getting composer to work. When not using composer autoloading, I can require_once the class file and instantiate an object. However, when attempting to use autoloading, I receive this error:
PHP Fatal error: Uncaught Error: Class 'MyCompany\User' not found in /var/www/html/portal/login.php:7
Stack trace:
#0 {main}
thrown in /var/www/html/portal/login.php on line 7
login.php looks like this:
<?php
require_once __DIR__ . "/vendor/autoload.php";
use MyCompany\User;
$user = new User();
The current composer.json looks like this:
{
"autoload": {
"psr-4": {
"MyCompany\\User\\": "vendor/mycompany/user/"
}
},
"require": {
"mycompany/user": "dev-master"
},
"repositories": [
{
"packagist.org": false
},
{
"type": "vcs",
"version": "master",
"url": "git#mysrc.example.com:/opt/git/phplib/classes/User.git"
}
]
}
The User.php file is this:
<?php
namespace mycompany;
class User {
function __construct() {
print "composer has done something\n";
}
} //end class User
?>
Finally, folder structure is:
portal\
composer.json
login.php
vendor\
mycompany\
\user\User.php
\pdo\PDO.php
\page\Page.php
Somehow namespacing seems to be the underlying issue but I'm not sure how to set it up in order to support this structure. Ultimately, even if the namespace is "mycompany" that would be fine but no combinations of namespace in the class, composer.json autoload statement, and use statement have worked. I have looked through countless questions here and none seem to match this exact scenario. This is on a linux system and I have also tried various combinations of upper and lower case for files and folders, though some fail because composer won't allow uppercase in certain areas.

Composer autoload doesn't find class

H, my composer json file autoload with psr-4 a Class, but when a Call that Class, php return error: Error: Class 'ClassA\Tae' not found
Here json autoload
"autoload": {
"psr-4": {
"ClassA\\": "includes/ClassA/",
"": "includes/"
}
}
and my php class that require ClassA is this
require_once __DIR__ .'/../vendor/autoload.php';
use ClassA\{ Rate, Tae, Taeg };
class TestTaeg extends \PHPUnit\Framework\TestCase {
public function test_tae() {
$obj = Tae::init( 5, 12 );
}
Do you know why I cannot find ClassA ?
I run the code with phpunit on cli, with this syntax (is the first time that I use phpunit)
../vendor/phpunit/phpunit/phpunit ./test-general.php
Thx
Are you namespacing your classes right ? To be honest your psr-4 autoloading looks a bit messy, I am asuming you are autoloading everything even when you dont need those.
So if you please share a screenshot of your folder structure, I can try to recreate your issue and probably suggest a solution.

Composer autoload not including my custom namespaces (Silex)

I'm developing a REST API with Silex and I'm facing a problem regarding autoloading of my custom librairy. It looks like Composer's autoload is not including it, because when I include it myself it works.
# The autoload section in composer.json
# Tried with :
# "Oc\\": "src/Oc"
# "Oc\\": "src/"
# "": "src/"
"autoload": {
"psr-4": {
"Oc\\": "src/"
}
}
<?php
// api/index.php <-- public-facing API
require_once __DIR__.'/../vendor/autoload.php';
$app = require __DIR__.'/../src/app.php';
require __DIR__.'/../src/routes.php'; // <--
$app->run();
<?php
// src/routes.php
// When uncommented, it works!
//include('Oc/ParseImport.php');
use Symfony\Component\HttpFoundation\Response;
use Oc\ParseImport;
$app->get('/hello', function () use ($app) {
return new Response(Oc\ParseImport(), 200);
});
<?php
// src/Oc/ParseImport.php
namespace Oc {
function ParseImport() {
return 'foobar!';
}
}
I run composer dumpautoload after each composer.json manipulation, and I do see the line 'Oc\\' => array($baseDir . '/src/Oc') (or anything I tried) in vendor/composer/autoload_psr4.php.
I can't figure out what is wrong.
Almost everything you did was correct.
When trying to autoload classes in a namespace, given that a class is named Oc\Foo and is located in the file src/Oc/Foo.php, the correct autoloading would be "PSR-4": { "Oc\\": "src/Oc" }.
However, you do not have a class. You have a function. And functions cannot be autoloaded by PHP until now. It has been proposed more than once (the one proposal I found easily is https://wiki.php.net/rfc/function_autoloading), but until now this feature hasn't been implemented.
Your alternative solutions:
Move the function into a static method of a class. Classes can be autoloaded.
Include the function definition as "files" autoloading: "files": ["src/Oc/ParseImport.php"] Note that this approach will always include that file even if it isn't being used - but there is no other way to include functions in PHP.
As illustration see how Guzzle did it:
Autoloading in composer.json
Conditional include of functions based on function_exists
Function definition

Class not found - PSR-4 namespaced autoloading

I wanted to set up PSR-4 autoloading for a class I wrote. However I keep getting the error Fatal error: Class 'Glowdemon1\Translxtor\LangParserXML' not found in C:\xampp\htdocs\translator\index.php on line 5
Folder structure(can't post img yet):
LangParserXML.class.php
namespace Glowdemon1\Translxtor;
class LangParserXML extends ErrorHandler implements ParserInterface{
...
index.php
require_once('vendor/autoload.php');
$translator = new Glowdemon1\Translxtor\LangParserXML('nl.xml');
composer.json
"autoload": {
"psr-4": {
"Glowdemon1\\": "src/"
}
}
autoload_psr4.php
return array(
'Glowdemon1\\' => array($baseDir . '/src'),
);
I have looked trough countless posts, yet no solutions. This is also posted on https://github.com/glowdemon1/translxtor in case you want a deeper look. Thanks.
Updates your composer.json to :
"autoload": {
"psr-4": {
"Glowdemon1\\Translxtor\\": "src/"
}
}
Or add a src/Transxtor/ directory before your LangParserXMl
Also, your filename cannot contain ".class". It should just be called LangParserXML.php.
I think you should have a Translxtor folder within src containing LangParserXML.class.php and Translator.class.php:
The contiguous sub-namespace names after the "namespace prefix" correspond to a subdirectory within a "base directory", in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.
Source: http://www.php-fig.org/psr/psr-4/
`

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