Class not found after composer require [duplicate] - php

This question already has answers here:
PHP class not found when using namespace
(2 answers)
Closed 3 years ago.
I'm learning php and composer following the article
I try to use external dependancy composer require phpunit/php-timer.
Here is my composer.json:
{
"name": "ypapax/composer_php_hello_world_log4php",
"minimum-stability": "dev",
"require": {
"php": ">= 7.2",
"phpunit/php-timer": "^2.1#dev"
},
"autoload": {
"psr-0": {
"HelloWorld": "src/"
},
"classname": {
"PHP_Timer": "src/"
}
}
}
and my test.php:
<?php
// Autoload files using Composer autoloader.
require_once __DIR__ . '/../vendor/autoload.php';
use HelloWorld\Greetings;
echo Greetings::sayHelloWorld();
Where greetings.php is
<?php
namespace HelloWorld;
use PHP_Timer;
class Greetings
{
public static function sayHelloWorld()
{
$timer = new PHP_Timer();
$timer . start();
return 'Hello World\n' . $timer->resourceUsage() . "\n";
}
}
When I run the test php tests/test.php
it gives me an error:
PHP Fatal error: Uncaught Error: Class 'PHP_Timer' not found in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php:11
Stack trace:
#0 composer_php_hello_world_log4php/tests/test.php(8): HelloWorld\Greetings::sayHelloWorld()
#1 {main}
thrown in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php on line 11
Fatal error: Uncaught Error: Class 'PHP_Timer' not found in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php:11
Stack trace:
#0 composer_php_hello_world_log4php/tests/test.php(8): HelloWorld\Greetings::sayHelloWorld()
#1 {main}
thrown in composer_php_hello_world_log4php/src/HelloWorld/Greetings.php on line 11
I guess something wrong is in composer.json:
"classname": {
"PHP_Timer": "src/"
}
PHP version:
$ php --version
PHP 7.3.9 (cli) (built: Sep 14 2019 18:07:55) ( NTS )
Link to my test repo
Update
Here is my file autoload_namespaces.php:
<?php
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'HelloWorld' => array($baseDir . '/src'),
);
And autoload_classmap.php:
<?php
// autoload_classmap.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'SebastianBergmann\\Timer\\Exception' => $vendorDir . '/phpunit/php-timer/src/Exception.php',
'SebastianBergmann\\Timer\\RuntimeException' => $vendorDir . '/phpunit/php-timer/src/RuntimeException.php',
'SebastianBergmann\\Timer\\Timer' => $vendorDir . '/phpunit/php-timer/src/Timer.php',
);

You are not loading the right namespace. I suggest you to checkout the examples on the package page

I think you don't need this in your composer.json:
"classname": {
"PHP_Timer": "src/"
}
According to https://github.com/sebastianbergmann/php-timer/blob/master/src/Timer.php you need
use SebastianBergmann\Timer\Timer as PHP_Timer;
in your greetings.php file.

The use statement was missing from the Greetings class:
<?php
namespace HelloWorld;
use SebastianBergmann\Timer\Timer;
class Greetings
{
public static function sayHelloWorld()
{
$timer = new Timer();
$timer::start();
return 'Hello World\n' . $timer->resourceUsage() . "\n";
}
}
and this can be removed from composer.json:
"classname": {
"PHP_Timer": "src/"
}

Related

Dependency injection cannot be handled using php-di

This is a problem that drove me crazy for over one week. I installed php-di via composer and added my own project to the composer.json file:
{
"name": "mypackage/platform",
"description": "MyPackage Platform",
"type": "project",
"require": {
"php-di/php-di": "^6.0"
},
"autoload": {
"psr-4": {
"MyPackage\\": "src/"
}
}
}
Then I created mine /public/index.php:
<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
use DI\ContainerBuilder;
use function DI\create;
use MyPackage\Base\Service;
$containerBuilder = new ContainerBuilder();
$containerBuilder->useAutowiring(false);
$containerBuilder->useAnnotations(false);
$containerBuilder->addDefinitions([
Service::class => create(Service::class)
]);
$container = $containerBuilder->build();
$service = $container->get('Service');
$service->showMessage();
And this is /src/Base/Service.php content:
<?php
namespace MyPackage\Base;
class Service
{
public function __construct()
{
}
public function showMessage()
{
echo "<BR>Inside Service";
}
}
When I troy to load index.php, Apache says:
[Thu Dec 05 18:57:47 2019] [error] PHP Fatal error: Uncaught DI\\NotFoundException: No entry or class found for 'Service' in /var/www/html/vendor/php-di/php-di/src/Container.php:135
Stack trace:
#0 /var/www/html/public/index.php(22): DI\\Container->get('Service')
#1 {main}
thrown in /var/www/html/vendor/php-di/php-di/src/Container.php on line 135
Whats is wrong with my approach?
$container->get('Service') asks the container for the entry 'Service'. This is a string. There is no namespace.
In your configuration, you configured the service under the Service::class key: Service::class returns the full class name, which is MyPackage\Base\Service.
So the error is: you have the service under MyPackage\Base\Service, and you try to fetch it via the key Service.

Class 'app' not found in bootstrap\app.php:4 #1 {main} thrown in C:\Users\Logan\Cart\bootstrap\app.php on line 4

I keep getting this error I checked the composer.json file and everything seems to be alright I'm following a tutorial for a shopping cart online in php. Any help would be greatly appreciated it.
I checked a similar question and he corrected in the composer.json file and that's not the issue with my problem.
bootstrap-app.php
<?php
session_start();
$app = new app;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../app/routes.php';
index.php
<?php
require __DIR__ . '/../bootstrap/app.php';
$app->run();
composer.json
"autoload": {
"psr-4": {
"Cart\\": "app/"
}
},
I've also have dumped the file before and no changes. I'm new to php and I'm just trying to complete this tutorial so I can complete an e-commerce website.
Fatal error: Uncaught Error: Class 'app' not found in
C:\Users\Logan\Cart\bootstrap\app.php:4 Stack trace: #0 C:\Users\Logan\Cart\public\index.php(3): require() #1 {main} thrown in C:\Users\Logan\Cart\bootstrap\app.php on line 4
Edited to include this:
<?php
namespace Cart;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DIBridge;
class App extends DIBridge
{
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefintions (
['settings.displayErrorDetails' => true,] );
$builder->addDefinitions(__DIR__ .'/container.php');
}
}

Problem to set up Composer / autoloading my classes

I'm learning how Composer is working (new to dev ^^) But i'm struggling to fix my autoloading...
here's my composer.json :
"autoload": {
"psr-4": {
"OCFram\\": "/../lib/",
"App\\": "/../",
"Model\\": "/../lib/vendors/",
"Entity\\": "/../lib/vendors/",
"FormBuilder\\": "/../lib/vendors/",
"Slug\\": "/../lib/vendors/"
}
},
So for example :
Fatal error: Uncaught Error: Class 'App\Frontend\FrontendApplication'
not found
Well, FrontendApplication path (from composer.json) : **
../App/Frontend/FrontendApplication.php
Here's the FrontendApplication.php with the namespace :
<?php
namespace App\Frontend;
use \OCFram\Application;
class FrontendApplication extends Application
{
public function __construct()
{
parent::__construct();
$this->name = 'Frontend';
}
public function run()
{
$controller = $this->getController();
$controller->execute();
$this->httpResponse->setPage($controller->page());
$this->httpResponse->send();
}
}
Plus, i've , noticed this file (autoload_psr4.php) on vendor/composer :
<?php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Slug\\' => array('/lib/vendors'),
'OCFram\\' => array('/lib'),
'Model\\' => array('/lib/vendors'),
'FormBuilder\\' => array('/lib/vendors'),
'Entity\\' => array('/lib/vendors'),
'App\\' => array('/'),
);
Would appreciate some help :)
[EDIT]
So i changed the path from
"App\": "/../" (which was no sense)
to :
"App\": "../",
NOW after another composer dump-autoload i get this :
<?php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Slug\\' => array($baseDir . '/../lib/vendors'),
'OCFram\\' => array($baseDir . '/../lib'),
'Model\\' => array($baseDir . '/../lib/vendors'),
'FormBuilder\\' => array($baseDir . '/../lib/vendors'),
'Entity\\' => array($baseDir . '/../lib/vendors'),
'App\\' => array($baseDir . '/..'),
);
But still same problem when i try php index.php i get :
Fatal error: Uncaught Error: Class
'App\Frontend\FrontendApplication' not found
As of your statement:
Well, FrontendApplication path (from composer.json) : **
../App/Frontend/FrontendApplication.php
You folder structure seems to be:
/App
/<some-dir>/composer.json
It seems you just missed App in the path, you don't need leading or trailing slashes.
"autoload": {
"psr-4": {
"OCFram\\": "../lib",
"App\\": "../App",
"Model\\": "../lib/vendors",
"Entity\\": "../lib/vendors",
"FormBuilder\\": "../lib/vendors",
"Slug\\": "../lib/vendors"
}
},

Fatal error: Class 'eftec\bladeone\BladeOne' not found

I am using PHP 5.5.9 and have installed the library BladeOne in my composer file:
{
"name": "test",
"authors": [
{
"name": "test",
"email": "test#test.com"
}
],
"require": {
"eftec/bladeone": "^3.0",
"davechild/textstatistics": "1.*",
"hassankhan/config": "^1.0"
}
}
I am running my script the following way:
<?php
require "vendor/autoload.php";
Use eftec\bladeone;
use DaveChild\TextStatistics as TS;
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
define("BLADEONE_MODE",1); // (optional) 1=forced (test),2=run fast (production), 0=automatic, default value.
$blade=new bladeone\BladeOne($views,$cache); <----- Here I get the error!
However, I get the following error here:
Fatal error: Class 'eftec\bladeone\BladeOne' not found in /home/ubuntu/workspace/testExample.php on line 10
Any suggestions why the library cannot be used in my script?
As per the docs you are supposed to add the namespace to the autoloading of composer by adding it to your composer.json.
"autoload": {
"psr-4": {
"eftec\\": "vendor/eftec/"
}
}
then, (again, the docs say) run composer update. I suppose though that composer dump-autoload would suffice.

psr4 autoload class not found

I am making my own framework but I have a problem with psr4 autoload and composer.
I can not make classes available and apparently everything is fine
This is the error, I don't want to use include for class files:
Fatal error: Uncaught Error: Class 'System\Template\get_template_files_class' not found in /var/www/html/fsib/system/layout-compiler.php:7 Stack trace: #0 {main} thrown in /var/www/html/fsib/system/layout-compiler.php on line 7
composer.json
{
"require": {
"illuminate/database": "*",
"illuminate/routing": "~5.1.16",
"illuminate/events": "~5.1.16"
},
"autoload": {
"psr-4": {
"System\\" : "system/libs/"
}
}}
layout-compiler.php
<?php
require_once '../vendor/autoload.php';
use System\Template\get_template_files_class;
$templates = new get_template_files_class;
?>
get_templates_files_class.php
<?php
namespace System\Template
class get_template_files_class {
}
?>

Categories