Composer psr-4 autoload cannot found file - php

I am still learning to use php composer
i have a directory structure like this :
Directory Structure
and this is my composer.json
{
"autoload": {
"psr-4": {
"Kct\\": "lib/"
}
}
}
Now in my index.php file i am trying to load Class tes in tesdir.php
<?php
// file: index.php
require __DIR__ . '/vendor/autoload.php';
$x = new \Kct\Tesdir\Tes();
var_dump($x->tes()); //output: 'GET'
my tesdir.php :
<?php
namespace Kct\Tesdir;
class Tes {
public function tes() {
return $_SERVER['REQUEST_METHOD'];
}
}
now if I open index.php in my localhost I got and error like this :
Fatal error: Uncaught Error: Class 'Kct\Tesdir\Tes' not found in /var/www/html/tesComposer/index.php:6 Stack trace: #0 {main} thrown in /var/www/html/tesComposer/index.php on line 6
can someone explain why.?

tesdir.php should be named Tes.php. The name of the file should match the name of the class.
See the PSR-4 examples

Related

Class not found while using composer and docker

Im using this library https://serp-spider.github.io/documentation/search-engine/google/
When I follow their example I got this.
Fatal error: Uncaught Error: Class "Serps\SearchEngine\Google\GoogleClient" not found in /var/www/html/index.php:19 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 19
This is my index.php
<?php
use Serps\SearchEngine\Google\GoogleClient;
use Serps\SearchEngine\Google\GoogleUrl;
$googleClient = new Serps\SearchEngine\Google\GoogleClient($httpClient);
$googleUrl = new GoogleUrl();
$google->setSearchTerm('simpsons');
$response = $googleClient->query($googleUrl);
$results = $response->getNaturalResults();
foreach($results as $result){
// Here we iterate over the result list
// Each result will have different data based on its type
}
?>
I've been thinking about what it could be for a while but I can't
I was missing the autoload, I added this line and works.
require 'vendor/autoload.php';
I changed the namespace and forgot to update it in composer.json
"autoload": {
"psr-4": {
"MyNamespace\\": "src"
}
},
Then run composer dump-autoload

PHP script can't work with autoloaded classes

The UsersView class object can't be loaded with the custom autoloader, finishing the PHP script with the following error. What would be the solution?
Fatal error: Uncaught Error: Class 'UsersView' not found in /Applications/XAMPP/xamppfiles/htdocs/solent/common/registration/pendings.php:4 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/solent/common/registration/pendings.php on line 4
Project structure:
classes
|_____ UsersView.php
common
|_____ autoloader.php
|_____ registration
|_____ pendings.php
Pendings.php script
include("../autoloader.php");
$pendingsView = new UsersView();
$rows = $pendingsView->getAllUsers();
Autoloader.php script
spl_autoload_register('myAutoLoader');
function myAutoLoader($className) {
$path = "../classes";
$extension = ".php";
$fullPath = $path . $className . $extension;
if(!file_exists($fullPath)) {
return false;
}
include_once $fullPath;

It will be displayed as Fatal error: Uncaught Error: Class 'eftec\bladeone' not found

I was trying to test BladeOne using PHP7.3.
composer.json
{
"name": "TEST",
"require": {
"eftec/bladeone": "^3.33"
},
"autoload": {
"psr-4": {
"eftec\\": "vendor/eftec/"
}
}
}
test.php
require "vendor/autoload.php";
Use eftec\bladeone;
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
$blade = new BladeOne($views,$cache,BladeOne::MODE_AUTO);
// $blade -> setAuth( ' johndoe ' , ' admin ' );
echo $blade->run("hello",array("variable1"=>"value1"));
This error appears when you run it.
Fatal error: Uncaught Error: Class 'eftec\bladeone' not found in /*/test.php on line 8
Error: Class 'eftec\bladeone' not found in /*/test.php on line 8
I also found a link like this but it didn't work.
Why does this error occur?
Replace Use eftec\bladeone; to use eftec\bladeone\BladeOne;.
And remove:
"autoload": {
"psr-4": {
"eftec\\": "vendor/eftec/"
}
}
From your composer.json.
Hope help you.
You included only the namespace, not the class name
Change your Use statement by this one :
use eftec\bladeone\BladeOne;
Or instantiate the class like this :
$blade = new bladeone\BladeOne($views,$cache,BladeOne::MODE_AUTO);
(Don't do both)

ZF2 Uncaught Error:Class 'ArrayUtils' not found in E:\Temp\htdocs\zf-tutorial\public\index.php:43

I am new at Zend2 and i am following the Album tuturial on Zend. I get the following error:
Fatal error: Uncaught Error: Class 'ArrayUtils' not found in E:\Temp\htdocs\zf-tutorial\public\index.php:43 Stack trace: #0 {main} thrown in E:\Temp\htdocs\zf-tutorial\public\index.php on line 43
I can't find whats the problem, what do i do wrong? Do i miss some code? Or do i miss some rules in the tutorial?
here is my index.php file
<?php
use Zend\Mvc\Application;
/**
* Display all errors when APPLICATION_ENV is development.
*/
if ($_SERVER['APPLICATION_ENV'] === 'development') {
error_reporting(E_ALL);
ini_set("display_errors", 1);
}
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
chdir(dirname(__DIR__));
// Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server') {
$path = realpath(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if (__FILE__ !== $path && is_file($path)) {
return false;
}
unset($path);
}
// Composer autoloading
include __DIR__ . '/../vendor/autoload.php';
if (! class_exists(Application::class)) {
throw new RuntimeException(
"Unable to load application.\n"
. "- Type `composer install` if you are developing locally.\n"
. "- Type `vagrant ssh -c 'composer install'` if you are using Vagrant.\n"
. "- Type `docker-compose run zf composer install` if you are using Docker.\n"
);
}
// Retrieve configuration
$appConfig = require __DIR__ . '/../config/application.config.php';
if (file_exists(__DIR__ . '/../config/development.config.php')) {
$appConfig = ArrayUtils::merge($appConfig, require __DIR__ . '/../config/development.config.php');
}
// Run the application!
Application::init($appConfig)->run();
To be able to use the Zend\Stdlib\ArrayUtils class, you have to either:
add a use statement at the top of your file: use Zend\Stdlib\ArrayUtils; or
change your ArrayUtils invocation to Zend\Stdlib\ArrayUtils

Autoload error in php

I am trying autoload function in php. The files are all in the same directory.
I have a file called aviation.php with the following code:
class Avaitor{
public function __construct(){
echo "Fly high<br>";
}
}
Then in my autoloader file I am trying to do this:
function __autoload($fileName){
if(file_exists($fileName . ".php"))
require_once $fileName . ".php";
}
//require_once "aviatior.php";
$pilot = new Avaitor();
But I am getting this error:
Fatal error: Uncaught Error: Class 'Avaitor' not found in
/Applications/MAMP/htdocs/php_oop/autoload.php:22 Stack trace: #0
{main} thrown in /Applications/MAMP/htdocs/php_oop/autoload.php on
line 22
Just to test that require_once does find my aviator.php I tried it out and then commented it out.
What am I doing wrong here?

Categories