Class not found issue (Composer, PHPUnit) - php

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.

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?

Class Not Found in PHP Laravel 6.x when another class extends

I always get the class not found exception in php laravel 6 when i create a class and extend a parent class named A which is located in the same directory.
However, another child class that is located in same directory could extend class A successfully.
In addition, i couldn't also instantiate the class A due to class not found exception in another .php file.
Please help me on this.
Thanks in an advance.
Parent class: myContext
<?php
namespace config\models;
class myContext {
public static $conn;
...
}
Class myUser: extension is fine.
<?php
namespace config\models;
class myUser extends myContext {
private $name;
...
}
Class oauth: extension returns myContext class not found.
<?php
namespace config\models;
class oauth extends myContext {
private $user;
}
Instantiate the class - returns class not found.
<?php
use config\models\myContext as context;
$cont = new context();
Check whether the namespace is added correctly when the parent class is imported.
Refer
https://www.quora.com/What-is-namespaces-and-use-in-Laravel
external classes and PHP files in Laravel - Class not found
Laravel - Model Class not found
Laravel 6: Class 'Form' not found
To get the provided example codes to work you need to use require_once
<?php
require_once('models/myContext.php');
use app\config\models\myContext as context;
$test = new context();
A search on SO brought me to this source
You try to add this line of code in the composer.json file, then execute the composer dumpautoload command on the command line
In composer.json file,
"autoload": {
"psr-4": {
"App\\": "app/",
"config\\models\\": "config/models"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
After that composer dump-autoload.
have you add autoload for config/ folder in composer.json. Laravel only default autoload for app/

Slim application Autoloading

I am newbie on REST and Slim applications.
I was not able to autoload some classes:
Type: RuntimeException Message: Callable UserController does not exist
File: /var/www/fdes/vendor/slim/slim/Slim/CallableResolver.php Line:
90
#0 /var/www/fdes/vendor/slim/slim/Slim/CallableResolver.php(61): Slim\CallableResolver->resolveCallable('UserController', 'login')
#1 /var/www/fdes/vendor/slim/slim/Slim/CallableResolverAwareTrait.php(45):
Slim\CallableResolver->resolve('UserController:...')
#2 /var/www/fdes/vendor/slim/slim/Slim/Route.php(351): Slim\Routable->resolveCallable('UserController:...')
composer.json:
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
Directory Structure:
fdes\app\api\Controllers\User\UserController.php
UserController.php:
namespace Api\Controllers\User;
use Slim\Http\Request;
use Slim\Http\Response;
use Respect\Validation\Validator as v;
class UserController extends BaseController {
protected $db;
Can u guys let me know what i am missing here?
Thanks Folks!
I think your namespace of your controller is wrong.
The namespace should be namspace App\Api\Controllers\User.
Furthermore it would be good if your folder structure would be identical to your namespace. So api should be Api, etc.

Class not found, composer and Zend Framework 1 autoloader issue

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
{

Symfony not finding a custom class

In my folder "/Vendor/User/Admin" I created a new custom class (Adminuser.php)
namespace \User\Admin;
class Adminuser {
public $username;
public $password;
}
Now Im trying to use it in a controller:
namespace Section\AdminBundle\Controller;
use \User\Admin;
class DefaultController extends Controller
{
public function indexAction()
{
$AdminUser = new \User\Admin\Adminuser(); // CLASS NOT FOUND!!
.......
Why is this happening?, the namespace is wrong? (I tried a few options..)
Im very begginer with Symfony, sorry.
You have 2 main issues.
The First
When declaring a namespace you should not start with a \
namespace \User\Admin;
Should just be:
namespace User\Admin;
The Second
If you want those classes to live in your Vendors Dir then you need to make sure the class is being autoloaded by symfony correctly. To do this we will use composer.
In your composer.json you will want to change this section from:
"autoload": {
"psr-0": { "": "src/" }
},
TO:
"autoload": {
"psr-0": { "": "src/" },
"psr-0": { "": "vendor/User/Admin" }
},
Then composer will add classes under that folder to the available namespaces and you will be able to access it as expected.
just remove the first "\" in the namespace, as the comments said. So the first file is:
namespace User\Admin;
class Adminuser {
public $username;
public $password;
}
if the problem persist check your autoloading configuration, maybe the right way would be using src dir to develop your code, not vendor :S

Categories