php json-schema - Fatal Error: Class not found - php

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));

Related

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

cannot find class, numerous libraries, composer php5.5

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.

Class not found error when try to use XBase lib in PHP

I am trying to use https://github.com/hisamu/php-xbase lib in my project.
I have copied XBase folder from repository to the my root and created index.php in my root.
When I try this code:
<?php
use XBase\Table;
$table = new Table(dirname(__FILE__) . 'data/test.dbf');
while ($record = $table->nextRecord()) {
echo $record->my_column;
}
?>
I received this error:
Fatal error: Class 'XBase\Table' not found in ...
What is wrong?
You are not requiring the file, that's why PHP can not find the class.
The example you see on GitHub, assumes you have installed and configured composer for your project.
To do this, download and configure composer, then run
composer require 'hisamu/php-xbase: *'
in root folder of your project. Then include vendor/autoload.php in your scripts. All installed classes using composer are now available.
Most frameworks do this for you, so you only use the class as mentioned. But when you are using a framework of yourself, or only plain PHP scripts, it's your responsibility to require autoload.php
Put these lines in the beginning of the table.php file:
include "Column.php";
include "Record.php";

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

Issue with PSR-0 autoloading

I am trying to use this composer package with a new project I am working on https://packagist.org/packages/activecollab/activecollab-sdk. However when i try and create a new class I keep getting the following errors.
Fatal error: Class 'ActiveCollab\Client' not found
The file that is throwing this error looks like this.
require "vendor/autoload.php";
new ActiveCollab\Client;
Which is just being used to test if the files are being loaded in properly. The composer.json of the file which I am trying to use looks like such. And I have a feeling the problem is in this file but I can't figure out what.
stuff...
"autoload": {
"psr-0": {
"ActiveCollab\\": "ActiveCollab"
}
}
...stuff
Also looking at the autload_namespaces.php file it is being generated as such.
<?php
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'ActiveCollab' => array($vendorDir . '/activecollab/activecollab-sdk/ActiveCollab'),
);
I have used psr-0 in some composer packages of my own and everything looks to be right except maybe the camel case in the namespace but i don't see this as being disallowed in the php proposal for psr-0.
Thanks for any help this has been driving me crazy.
The thing is: You cannot simply add a composer.json file with a random autoloading configuration and hope that it works - it actually has to match the naming scheme you are using. That is what this project got wrong, and nobody tested it. Which probably means nobody uses this library, and you can expect no support from the creators due to lack of interest.
But let's see how they react on my pull request to get things back to working again.
The composer config looks fine: Is it just the case that you omitted the leading \ from your class name?
new \ActiveCollab\Client;
You'll need that if your code is inside another namespace, as it will load it relative to the current namespace.
EDIT: I've just checked out that library, and even with the above fix, the autoloader wasn't quite working. The autoloader may also be broken due to the composer.json file for the library specifying a PSR0 autoloader, but using ".class.php" extensions (not PSR0 compatible). An autoload.php file is included with the library, so if you just require that file, you should be able to use the classes:
require 'vendor/activecollab/activecollab-sdk/ActiveCollab/autoload.php';
After doing this, I was able to use the class.

Categories