cannot find class, numerous libraries, composer php5.5 - php

I am trying different libraries via composer and on all of them I hit a similar error:
Fatal error: Class 'MyRestApi\Admin' not found in /usr/home/www/tmp/index.php on line 6
I have tried short and long path names:
set_include_path('/usr/home/www/vendor/marcj/php-rest-service/RestService');
require_once('/usr/home/www/vendor/marcj/php-rest-service/RestService/Server.php');
I have tried require, include, require_once, set_include_path.
Also I have tried
require "../vendor/autoload.php";
and
require "/home/www/vendor/autoload.php";
Always the same error trying to find the first class.
This particular library https://github.com/marcj/php-rest-service it the one I am trying.

How do you get a composer library to work without an autoload? (I am assuming this has something to do with my issue)
You don't. Using the autoloader of Composer is one of the central contracts that every package that gets installed with Composer relies on.
In theory you could try to invent your own autoloading. In practice: Why?
Your code is missing a require "vendor/autoload.php"; - the path to vendor depends on where your file is that wants to include the autoloader.
Note that if you add your own package and install it via Composer, it must not include the autoloader by itself - the code that uses this package is supposed to do it.

Related

php json-schema - Fatal Error: Class not found

New to PHP!
I have /var/www/html/index.php requiring json-schema from https://github.com/justinrainbow/json-schema
downloaded from git and moved JsonSchema folder to /var/www/html
the following in index.php gives Fatal error: Class 'JsonSchema\Constraints\Constraint' not found
require "JsonSchema/Validator.php";
use JsonSchema\Validator;
$validator = new JsonSchema\Validator();
$validator->check(json_decode($data), json_decode($schema));
if I include Constraint.php, it throws out another error. am missing some basics here. what is the proper way to use an external library?
thanks!
If you look in the project root, then there's a special file called composer.json wherein you will find on line 46 the namespace autoloader.
"autoload": {
"psr-4": { "JsonSchema\\": "src/JsonSchema/" }
},
When you install your project with composer, then this will generate a file called autoload.php which once included in your script will allow you to access all of the classes. Otherwise you are doomed to requiring each class one by one.
Furthermore, requiring each class is really inefficient on memory usage and runtime, so composer's autoload.php uses spl_autoload_register which is even better because it only loads the classes when they're actually called. Otherwise if you require a ton of classes and don't use all of them, then it's just a waste of resources and slows things down.
First thing you will need is composer
wget http://getcomposer.org/composer.phar
Usually people will use composer for downloading and including packages like this one as a new project dependency.
php composer.phar require justinrainbow/json-schema:~2.0
But since you've already cloned the source code because you want to actually develop this package, then you can simply generate the autoloader with:
php composer.phar dump-autoload
So your script should look like this:
require __DIR__ . '/vendor/autoload.php';
use JsonSchema\Validator;
$validator = new JsonSchema\Validator();
$validator->check(json_decode($data), json_decode($schema));

Symfony - Autoloader can't find class / composer error

When I tried adding a new dependency to my project with composer require xyz I got the following error:
The autoloader expected class "PackageVersions\Versions" to be defined in file ".../vendor/ocramius/package-versions/src/PackageVersions/Versions.php".
The file was found but the class was not in it, the class name or namespace probably has a typo.
I dug into that file, to see that the Versionsclass is there, in the right file, but with a the following name:
Versions_composer_tmp0
The namespace declarations seem to be good in the entire project, as well as php opening tags (I read that might cause such problems).
Additionally, I remarked that all the use statements in the Installer class file, which is the one that creates the Versions class are marked by phpstorm as Undefined Classes. They all should be found in the namespace Composer\xyz.
I tried the following without succes:
clearing the symfony cache
clearing composer cache
composer self-update
deleting the ocramius vendor folder so that composer would download it again
renaming the class, which is pointless since the entire purpose of this Versions class is to be rewritten with each composer install or composer update
edit:
I'm trying to install 1up-lab/OneupUploaderBundle, the Ocramius/PackageVersionsis already there as a dependency probably (I did not require it manually)
edit 2:
I just saw that server:run won't work either. So the problem is definitely not related to the bundle I'm trying to install. I managed to make the server run by renaming the class from Versions_composer_tmp0 to Versions.
It turns out that this is a composer issue:
composer/composer#5237
Ocramius released a fix/workaround for this:
Ocramius/PackageVersions - Release 1.0.4

Reference .phar classes installed globally with composer

I've created a small command line tool in PHP and I've also packed that as a PHAR archive.
Next thing I did was publish my archive to packagist.org aka composer.
I can now install my PHAR package through composer like so:
composer global require acme/mypackage
This installs my package fine. And I'm able to run it through command as well.
So far so good, but here comes the problem I´m currently facing.
I have another project should use acme/mypackage. I want that project to reference a class that is packed into that PHAR. Something like this:
<?php
class SomeClass extends AcmeClass {
}
The problem is that the PHP code doesn't recognize the AcmeClass class. Makes sense, because it´s obviously "globally" installed somewhere on the system.
How do other libraries solve this issue? If I'm not mistaken then PHPUnit does something similar right?
How can I solve this issue?
You'll need to add a composer.json file to the root of your project:
The first (and often only) thing you specify in composer.json is the require key. You're simply telling Composer which packages your project depends on.
{
"require": {
"monolog/monolog": "1.0.*"
}
}
Next, you'll need to autoload your dependencies.
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can simply include this file and you will get autoloading for free.
require 'vendor/autoload.php';
https://getcomposer.org/doc/01-basic-usage.md

PHP Zend library results in Fatal Error: class 'Zend\[whatever]' not found

I'm hoping someone can spot what I've forgotten to do. Here are my steps:
Downloaded and unpacked the ZendFramework-2.3.5 into /usr/share.
Updated include_path in my php.ini file to include '/usr/share/ZendFramework-2.3.5/library' per the INSTALL.md, and restarted Apache to confirm the path is set (now ".:/usr/share/php:/usr/share/ZendFramework-2.3.5/library").
Created a test script in my web document root (using the class 'CamelCaseToUnderscore' as an example):
use Zend\Filter\Word\CamelCaseToUnderscore;
$filter = new CamelCaseToUnderscore();
echo $filter->filter('BigsAndLittles');
...and I get the fatal error "class 'zend\filter\word\camelcasetoseparator' not found".
In order to do use Zend classes like this, do I need to do some additional configuration or create an autoloader or something to find them? Seems like this should have worked. If I include the CamelCaseToUnderscore.php file in a require_once statement, then I get a fatal error that it's parent class doesn't exist (CamelCaseToSeparator.php). What am I missing?
You can use require 'Zend/Mvc/Application.php' to test if your include path is correct, but you will need an autoloader:
http://framework.zend.com/manual/current/en/modules/zend.loader.standard-autoloader.html.
You can find an example here (lines 18-20):
https://github.com/zendframework/zf2/blob/master/demos/Zend/Feeds/consume-feed.php
I strongly suggest using composer as it will save you a lot of time troubleshooting your include paths, but it also allows you manage version better. It makes it easier for other developers and to deploy your code.
Starting with composer is very easy, just install it and create composer.json:
https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup
Run:
composer require zendframework/zendframework
Composer will download all libraries to vendor folder and will generate an autoloader, all you have to do is to include
require 'vendor/autoload.php';
https://getcomposer.org/doc/01-basic-usage.md#autoloading
Most popular PHP frameworks use composer for managing dependencies:
https://github.com/zendframework/zf2/blob/master/composer.json
https://github.com/symfony/symfony/blob/2.7/composer.json

aws-php-sdk -- Installing without a package manager

I'm working on pulling in the AWS PHP SDK and am running into some issues since my current stack isn't using a package manager. It's not an option to start using it, either (company related -- would rather not elaborate).
That all being said, I'm pulling in the source directly and trying to add it to my include path and just including the files as I need them in my S3 wrapper objects that I'm writing. However, it's running into issues with the namespaces (I think) and those blowing up.
This is the library I'm referring to:
https://github.com/aws/aws-sdk-php
I tried following the bit at the bottom about working with AmazonS3 and uploading a file to it. So, I attempted to include the various parts of the code it referenced as follows:
Attempt One
require_once('/includes/third_party/aws-sdk-php-master/src/Aws/Common/Aws.php');
require_once('/includes/third_party/aws-sdk-php-master/src/Aws/S3/Enum/CannedAcl.php');
require_once('/includes/third_party/aws-sdk-php-master/src/Aws/S3/Exception/S3Exception.php');
Attempt Two
set_include_path(get_include_path() . "/includes/third_party/aws-sdk-php-master/src/");
include('Aws/Common/Aws.php');
include('Aws/S3/Enum/CannedAcl')
include('Aws/S3/Exception/S3Exception.php');
Both of these produced a similar error:
Fatal error: Class 'Guzzle\Service\Builder\ServiceBuilderLoader' not found in \includes\third_party\aws-sdk-php-master\src\Aws\Common\Aws.php on line 26
PHP Fatal error: Class 'Guzzle\Service\Builder\ServiceBuilderLoader' not found in \includes\third_party\aws-sdk-php-master\src\Aws\Common\Aws.php on line 26
Any advice on how to start debugging this? Would be much appreciated!
The AWS SDK for PHP nows ships a zip file with everything you need, including an autoloader: http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/installation.html#installing-via-zip.
You should stick the the recommend installation procedures. Personally, I would go with the Composer installation or just use the PHAR without Composer.
Then you just need to include the PHAR like:
require '/path/to/aws.phar';
And you will have everything you need.
The problem you have now is likely that you are not taking advantage of the autoloader. Using your approach, you will need to manually include all the classes that would normally be autoloaded.
You also need to download and include another library (Guzzle, not included in the PHAR archive).

Categories