Zend Framework 2 claims to have a "use-at-will" design that allows you to easily use any of its modules without committing to the full stack. I need a good database access layer, and from the docs and recommendations online I like the look of Zend\Db. I've placed the Zend/Db folder in my /lib directory, but I'm having trouble getting PHP to recognize the Zend\Db\Adapter\Adapter class. I keep getting a fatal error when I try to use it:
Fatal error: Class 'Zend\Db\Adapter\Adapter' not found in /home/username/public_html/test.php on line 6
I've tried setting ZF2_PATH in my .htaccess:
SetEnv ZF2_PATH /home/username/public_html/lib/Zend
I've tried setting the include path in my code:
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/lib' . PATH_SEPARATOR . get_include_path() );
I've tried explicitly loading and instantiating the Zend\Loader:
require_once 'lib/Zend/Loader/StandardAutoloader.php';
$zendLoader = new Zend\Loader\StandardAutoloader();
$zendLoader->register();
None of these have had any effect. I tried explicitly requiring Zend/Db/Adapter/Adapter.php, and that did fix the error I'm seeing, but then I just get the same error for one of its dependencies, so that's not a practical solution.
What am I doing wrong here? Is ZF2 just not really designed for this kind of modular usage, or am I missing something?
EDIT: I got this to work by writing my own autoload function:
function autoloader($class) {
$path = explode('\\', $class);
foreach ($path as $p) {
$cp .= DIRECTORY_SEPARATOR . $p;
}
include __DIR__ . '/lib/' . $cp . '.php';
}
spl_autoload_register(autoloader);
This sort of makes sense -- obviously if I'm using the db module without the rest of the framework, I can't expect the framework to do autoloading for me -- except I still don't understand why manually loading Zend\Loader didn't solve the problem. Isn't handling autoloading the point of Zend\Loader? Anyway, I have a workable solution for now, but if there's a better solution I'd love to hear it.
I strongly recommend you check out composer. It really simplifies managing dependencies between a ton of modern PHP libraries, as well as autoloading.
For instance, if you were starting you project, and you knew you wanted to pull in zend-db, you'd do something like this:
$ mkdir myproject
$ cd myproject
$ curl -s https://getcomposer.org/installer | php
$ ./composer.phar require zendframework/zend-db:2.1.1
That last line will cause composer to spring into action. It will create a directory called "vendor" where it keeps all the libraries it manages. It then will check out version 2.1.1 of zend-db in there, and set up an vendor/autoload.php which you'll require in your project.
Then you can test it out. Create myproject/index.php like so:
<?php
require_once "vendor/autoload.php";
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Pdo_Sqlite',
'database' => 'path/to/sqlite.db'
));
And it just works.
Later, if you decide you need Zend\Mail too, you just:
$ ./composer.phar require zendframework/zend-mail:2.1.1
and composer will install it, along with a couple of dependencies, and make sure it's available to the autoloader.
Composer is not ZF-specific, either. There's a whole ecosystem of code to explore. Packagist is a good place to start.
Related
I'm new on amphp and i'd like to try this very simple code first.
I downloaded amphp with composer for windows and save all folder created inside my project folder.
composer require amphp/http-client
Then my code is:
<?php
require __DIR__ . './autoload.php';
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Loop;
$stringa = 'http://www.google.it/';
$request = new Request($stringa, "GET");
$location = $request->getHeader("Location");
var_dump($location);
But I get always 'Fatal error: Uncaught Error: Class 'Amp\Http\Client\Request' not found'
Any suggest?
I use wamp local server with php 7.0
Also ,after, I need to yield all the code...
Here are a few things that look for:
Did the autoload.php get included correctly?
I see there is an unnecessary . before /autoload.php.
Did you use the correct class name with the correct namespace?
Did you run composer require amphp/http-client so that the libraries will be installed?
Do the library's files exist inside the vendor/amphp/http-client directory?
If you're on Windows, use \ in the require statement.
Apart from these, I can't think of why the library won't load. I hope this helps.
I am work on legacy app that includes dozens and dozens of different php scripts, all written before Composer was widespread in use.
I needed to use a package (for sending email) only available through Composer, so I created a composer directory and included it into one script like so:
include_once("/home/soccer/src/autoload.php"); // the old autoloader
require '/home/soccer/src/Mailgun/vendor/autoload.php';
use Mailgun\Mailgun;
Now the problem is that Composer's autoload (shown in line 2) works fine, and I can send mail, however now the important libraries from line 1 do not work:
PHP Fatal error: Class 'DB' not found in .....
There is a file at:
/home/soccer/src/DB.php
that is the most basic MySQL db loader imaginable. I tried adding a namespace to it with:
namespace Footyscores;
and then in the PHP script adding **Footyscores** to the function call, but no luck there, same error.
Here is the legacy autoloader:
include_once "/home/soccer/src/settings.php";
function __autoload($class)
{
global $srcDir;
$parts = explode('\\', $class);
$file = end($parts);
include_once $srcDir . $file . '.php';
}
How is I fix this? Thanks!
Im kind of new with Travis, and I am expreimenting with it right now. I uploaded have my PHP Project on Github and when I let it test via Travis it fails and gives me this error.
PHP Fatal error: Class 'controllers\Welcome' not found in /home/travis/build/ezylot/PHPSkeleton/tests/controllers/welcomeTest.php on line 4
I use a autoloader to load the classes, and it is no problem on my local machine. I include the autoloader in bootsrap.php with the bootstrap in the PHPUnit Konfiguration-XML File.
<?php
if (!#include __DIR__ . '/../vendor/autoload.php') {
die('You must set up the project dependencies, run the following commands:
wget http://getcomposer.org/composer.phar
php composer.phar install');
}
?>
You are most likely developing on OSX which has case insensitive filesystem and tests pass. Travis uses case sensitive file system. Try renaming app/controllers/welcome.php to app/controllers/Welcome.php.
In general it is good idea to follow PSR-1 standard to avoid autoloading issues.
I had a short php open tag at the top of the class file.
<?
as opposed to
<?php
This broke it on the remote, but not on my local. Which is weird, because I would've expected it to break locally too.
Putting this out there in case someone else is in the same odd situation.
I installed composer in my CodeIgniter project, downloaded 2 packages: Aura/Sql and Aura/SqlQuery
this is my code from index.php file
require_once ROOTPATH . 'vendor/autoload.php';
use Aura\Sql\ExtendedPdo;
$db = new ExtendedPdo('mysql:host=127.0.0.1;dbname=mydb', 'root', '', array(), array());
var_dump($db->fetchAll('SELECT * FROM sh_users'));
use Aura\Sql_Query\QueryFactory;
$query_factory = new QueryFactory('mysql');
require_once BASEPATH . 'core/CodeIgniter.php';
both fragments are copied from documentation
var_dump gives perfect result, but QueryFactory gives me error
Fatal error: Class 'Aura\Sql_Query\QueryFactory' not found in F:\XAMPP\htdocs\codeigniter\public\admin\index.php on line 83
and i have no idea why. all vendors are downloaded and all php files are there, but it seems autoload doesnt load it. why?
Take a look at the file structure on disk; you may actually want to be including Aura\SqlQuery\QueryFactory, instead of something under the Sql_Query namespace. It may be something as simple as that. I've encountered issues when I've forgotten to rename the class in a PSR-0 compliant path such that it matches the file name, so if in fact the contents on the disk are in:
Aura\SqlQuery\QueryFactory but your use statement is Aura\Sql_Query\QueryFactory, you'll run into a problem.
As mentioned below in the comments, it appears that the Aura devs have two branches, the master branch on the Githup project auraphp/Aura.Sql_Query still has the directory structure as Sql_Query where as the default package, served by packagist, serves the dev-rename branch which replaces Sql_Query with SqlQuery.
Hope that helps!
I am trying to install xml diff ; https://github.com/mmacia/XMLdiff and i have not managed yet to make it work.Whenever i run any test example,i get
Fatal error: Interface 'PHPUnit_Framework_Test' not found in
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php on line 85
Has anyone managed to install and use the library.I am using xampp on windows.
I believe your problem has to do with PHPUnit's Autoloader.php not being included. This file sets the php spl_autoloadspl_register function which is responsible for loading in interfaces and classes like PHPUnit_Framework_Test.
According to this SO question, you have to include the autoloader file manually. Without knowing more about your set-up and how that particular library works, I would say do something like this in the appropriate file(s):
// define phpunit path
if ( ! defined('PHPUNIT_PATH')) {
// define an absolute path to your PHPUnit dir
// CHECK THIS, i'm not good with php on windows:
define('PHPUNIT_PATH','C:\xampp\php\PEAR\PHPUnit');
}
// Then include the autoloader like this:
include PHPUNIT_PATH.'Autoloader.php';
I hope this helps you or anyone else out.
Check execution flags for C:\xampp\php\PEAR\PHPUnit\Framework\Framework\Test.php
The file needs to be executable by the user who is launching tests (probably you).