Composer autoloader not loading classes - php

I am building a tiny vanilla PHP application and need to implement an autoloader into the functionality as I want to ensure my application is coded in a clean and efficient manner. I have decided to use Composer for it's built in autoloading functionality and have installed this and created a composer.json file. I then ran:
composer install
I have a class that is namespaced under lib\DB as follows:
<?php
namespace lib;
class DB
{
.....
And am attempting to call this namespaced class using:
include_once('config.php');
use lib\DB;
function addProducts() {
try {
$db = DB::getInstance();
$connection = $db->getConnection();
This is throwing an error saying:
Fatal error: Uncaught Error: Class 'lib\DB' not found in /var/www/php-parser/index.php on line 9
I can see the autoload.php files has been generated inside the vendor folder so this is all ok. Is there something I have forgotten to do?
Thanks

Related

Change namespace class autoload to manual require_once PHP

I want to use namespaces class to manual one without using autoload.php to be included. Because I don't want to use all function the class.
I am using this project https://github.com/codenix-sv/coingecko-api to get it's function in my php function.
In the example of using is like this
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
$client = new CoinGeckoClient();
$data = $client->ping();
But I want to change it to require_once. So I put all src folder in my php folder and create this to call the function
require_once 'libs/Api/CoinGeckoClient.php';
$client = new Codenixsv\CoinGeckoApi\CoinGeckoClient;
$data = $client->simple();
First I got this error when trying to access the page.
Fatal error: Uncaught Error: Class 'GuzzleHttp\Client' not found in
C:\xampp\htdocs\te.st\libs\Api\CoinGeckoClient.php:35
Then I try to remove the line "use GuzzleHttp\Client" in CoinGeckoClient.php file.
And got with this error
Fatal error: Uncaught Error: Class 'Codenixsv\CoinGeckoApi\Client' not
found in C:\xampp\htdocs\te.st\libs\Api\CoinGeckoClient.php:35
Is there any way to just use the "simple" function of coingecko only in my php file.
https://github.com/codenix-sv/coingecko-api/blob/master/src/Api/Simple.php
Here is the way I fix this.
load in composer.json like
{
"require": {
"codenix-sv/coingecko-api": "^1.0",
"guzzlehttp/guzzle": "~6.0"
}
}
then do composer update in command window.
In my php file. make sure
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
is placed in top of file. Then do the rest.
Thanks all
That package is prepared to works with composer.
Composer delivered autoloader, to make you work simplier.
If you remove use GuzzleHttp\Client line from CoinGeckoClient.php, there will be no way to send request to the server.
The best option is include composer autoload in your project file, it means you should:
Create composer.json file for you project
Add required library dependency using command: composer require guzzlehttp/guzzle
Add require library dependency using command: composer require codenix-sv/coingecko-api
Inside your project file add folowing line:
require_once(dirname(__FILE__) . '/vendor/autoload.php');
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
$client = new CoinGeckoClient();
$data = $client->ping();
In other way, that will be necessarily to import all files manually. And of course, you haven't to forget about imports for Guzzle client.

PHPUnit does not find a class

I am working on a new tokenizer class for PHP_CodeSniffer. This also requires making a new Standard since the valid tokenizers are specified within each standard.
My new classes are using the same namespaces as the “sibling” classes from the original codebase.
If I use composer to install my project, and run phpcs, the auto loader seems to pick up my new classes just fine. PHP_CodeSniffer will load my tokenizer by calling:
$className = ‘PHP_CodeSniffer\\Tokenizers\\’ . $type;
$class = new $className(<stuff>);
Similarly, my custom standard has a use statement for a class in the original codebase. When running phpcs, no error are thrown.
When I try to run a test file through phpcs in a unit test, I get an error that my custom sniff file is unable to find the class in the use statement. My guess is this is auto-loading related. The bootstrap.php file for phpunit requires __DIR__ . ‘/../vendor/autoload.php’.My project composer.json has:
“autoload”: {
“psr-4”: { “PHP_CodeSniffer\\”: “src\” }
},
“autoload-dev”: {
“psr-4”: { “PHP_CodeSniffer\\Tests\\”: “tests\” }
}
Is there an issue with using the same namespace as the original code? With registering the same namespace to a different location in composer? Why does it work in runtime but not in testing?
It turns out PHPUnit has its own custom bootloader. When I added a require_once() for that in my PHPUnit bootloader, it loaded successfully. Thanks to a user on the PHP subreddit.

Cannot redeclare class error in Laravel but works fine in native php

I am using Alchemy API for filter out some data. Everything works fine in the native code. But when i used it in my Laravel Controller it throws Cannot redeclare class. My controller,alchemyapi.php and example.php are in the same directory. Here is how i include alchemyapi.php in native code
<?php
require_once 'alchemyapi.php';
$alchemyapi = new AlchemyAPI("MY API KEY"); ?>
But when i include it in the controller it throws my the error. is there something i am missing ?
require_once 'alchemyapi.php';
$alchemyapi = new AlchemyAPI("MY API KEY");
The native code(example.php) works well without any issue. But in laravel controller it throws a error saying Cannot redeclare class AlchemyAPI
in alchemyapi.php line 20
Instead of using require_once use namespace in your alchemyapi.php and then use use for same namespace in your MyController
alchemyapi.php
<?php
namespace App\Http\Controller;
class AlchemyApi {
//your code
}
MyController.php
<?php
namespace App\Http\Controller;
use App\Http\Controller\AlchemyApi;
class MyController {
$alchemy = new AlchemyApi("Your_api_key");
}
OK, so what I think is happening is that alchemyapi.php is somehow already being included by the composer autoloader.
Try this.
Create the directory lib in the root of your project.
Move alchemiapi.php into lib.
Under the "autoload" section in your composer.json add the following code. Make sure the JSON is valid:
"files": [
"lib/alchemyapi.php",
],
Run composer dump-autoload. If it errors, your composer.json is invalid or you haven't put the file in the correct place.
Delete require_once 'alchemyapi.php'; from your controller.
When dealing with composer, this is how you deal with classes in the global namespace. After running composer it scan those directories in app for PSR-4 classes.
I can't be sure but I think that composer is looking for it and you are also manually requiring it. That would explain why PHP thinks you are redeclaring the class.

PHPUnit CodeIgniter Generate Fixtures PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found

So have PHPUnit and CodeIgniter installed:
http://d.hatena.ne.jp/Kenji_s/20120117/1326763908
Couldn't download the PEAR as its been deprecated. So had to download the phpunit phar file:
http://phpunit.de/manual/4.0/en/installation.html#installation.phar
So was able to get some tests to run properly. Moved my phpunit.phar to /usr/local/bin and ran on the tests dir:
php /usr/local/bin/phpunit.phar
And all the tests ran correctly. But when i tried to run the php generate fixtures and php generate.php fixtures:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /www/test/application/third_party/CIUnit/libraries/CIUnitTestCase.php on line 15
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /www/test/application/third_party/CIUnit/libraries/CIUnitTestCase.php on line 15
Seems like its not finding the classes inside the phar file or at least they are not in the correct order? What is funny is that it runs the tests fine but not the generate fixtures.
Additionally i also installed using composer the phpunit so i have a /www/test/vendor/bin/phpunit installed as well.
Any help would be appreciated.
I had the same problem in my code, although I do not use the CodeIgniter. Trying to run tests would result in the error message:
Class 'PHPUnit_Framework_TestCase' not found
For what it's worth I had this fixed by adding a backslash to my test class declaration.
// Before
namespace IMAVendor\Super\Duper;
class MyClassTest extends PHPUnit_Framework_TestCase
// After
namespace IMAVendor\Super\Duper;
class MyClassTest extends \PHPUnit_Framework_TestCase
^
Added this backslash here
This seems to have something to do with namespaces and the autoloader that's built in phpunit. I have my own autoloader for the project code and it seems that it was trying to load the phpunit's classes from my code. I'm not really sure why it didn't try to load it from the 'base' when it wasn't able to find it in the projects namespace (This may very well be due to my own autoloader being faulty).
I know this is an old question, but I'll just leave this here in case it may help somebody somewhere.

Using Unirest php lib that has a namespace

I'm trying to use unirest, a new php lib for making rest calls.
I'd like to place it in a system-wide directory above my project. I then include it:
require_once ('../unirest-php-master/lib/Unirest/Unirest.php');
loads fine. Then I use it per the readme:
$response = Unirest::post(CSWA_URL ....
I get Fatal error: Class 'Unirest' not found in ...hello_world/sign_start.php on line 23
I then try to use the namespace (see the library's code. They use a Namespace Unirest statement before declaring the Unirest class.)
$response = Unirest\Unirest::post(CSWA_URL ....
I got further. Now: Fatal error: Class 'Unirest\HttpMethod' not found in ....unirest-php-master/lib/Unirest/Unirest.php on line 26 -- This is an error in the library code!
Q: Did I do something wrong? Did the authors of Unirest make a mistake? Do I have to place the library in ./lib? What's the best fix?
It looks like the Unirest code in Unirest.php relies on autoloading code from the two other files in the unirest lib directory (HttpMethod.php and HttpResponse.php).
The author suggests installing the package using composer, if you were to do that composer would add the Unirest namespace to the autoloader.php script it generates. From there you need to require the autoload.php file at the top of your script and it will handle loading classes that aren't defined.
Alternatively, if you don't want to use composer, I would just require the other two files in the unirest lib directory at the top of your script as well.

Categories