I dowload the last versión of Mustache (2.7) with Composer,
"require": {
"mustache/mustache" : "2.7.*",
// etc...
}
but when I try:
use Mustache\Mustache_Autoloader;
abstract class BaseController {
public function __construct() {
Mustache_Autoloader::register();
/...
}
/...
}
the error.log said:
PHP Fatal error: Class 'Mustache\\Mustache_Autoloader' not found in
Although, Mustache_Autoloader hasn't namespaces.
Composer has: composer/autoload_namespaces.php:
return array(
'Mustache' => array($vendorDir . '/mustache/mustache/src'),
//etc
);
And in my main file I don't forget include require 'vendor/autoload.php'; But I don't know what happend. Any idea? Thanks.
SOLUTION:
Only I need to add '\' at the beginning of the word. like new \Mustache_Engine().
Now it works. Thanks for your help :)
First, why do you want to use the Mustache\Mustache_Autoloader ?
composer should take care of the autoloading.
Further i see in https://github.com/bobthecow/mustache.php/blob/master/src/Mustache/Autoloader.php
that this class has no namespace.
Therefor use Mustache\Mustache_Autoloader; fails.
If you want to use the autoloader you better use:
require '/path/to/mustache/src/Mustache/Autoloader.php';
Mustache_Autoloader::register();.
Related
I'm using composer to create an autoload file, and all that jazz works as expected.
The problem is: when I try to call a globally defined class, it keeps looking inside the namespace, instead of the global scope. Even though I use the \ escape as in "new \WC_Order();"
I've configured composer.json to autoload MyNamespace in the correct dir and include 'vendor/autoload.php' in my wordpress theme's function.php, and create a new instance, which works. Everything about this process works.
include 'vendor\autoload.php';
$myclass = new MyNamespace\MyClass()
Here's where the error occurs in the file 'MyNamespace\MyClass.php:
namespace MyNamespace;
class MyClass {
public function __construct()
{
$order = new \WC_Order(1234);
}
}
PHP throws a fatal error:
Fatal error: Uncaught Error: Class 'MyNamespace\WC_Order' not found in ...\MyNamespace\MyClass.php
Any tips or hints would be greatly appreciated!
Edit: My composer/directory setup
composer.json
{
"autoload": {
"psr-4": {
"MyNamespace\\": "MyNamespace"
}
},
"require": {
"guzzlehttp/guzzle": "^6.3"
}
}
functions.php is in the root and the class is in MyNamespace/MyClass.php
I figured it out. Since this is Wordpress, I should have hooked into an action. Woocommerce hadn't been loaded yet. As soon as I used add_action('init', 'doMyStuff'); it worked.
I still find it strange that the message was that it couldn't find the class inside the namespace though.
Try:
include 'vendor\autoload.php';
$myclass = new \MyNamespace\MyClass();
Or:
include 'vendor\autoload.php';
use MyNamespace\MyClass;
$myclass = new MyClass();
You can find a guide here: https://www.php.net/manual/en/language.namespaces.importing.php
H, my composer json file autoload with psr-4 a Class, but when a Call that Class, php return error: Error: Class 'ClassA\Tae' not found
Here json autoload
"autoload": {
"psr-4": {
"ClassA\\": "includes/ClassA/",
"": "includes/"
}
}
and my php class that require ClassA is this
require_once __DIR__ .'/../vendor/autoload.php';
use ClassA\{ Rate, Tae, Taeg };
class TestTaeg extends \PHPUnit\Framework\TestCase {
public function test_tae() {
$obj = Tae::init( 5, 12 );
}
Do you know why I cannot find ClassA ?
I run the code with phpunit on cli, with this syntax (is the first time that I use phpunit)
../vendor/phpunit/phpunit/phpunit ./test-general.php
Thx
Are you namespacing your classes right ? To be honest your psr-4 autoloading looks a bit messy, I am asuming you are autoloading everything even when you dont need those.
So if you please share a screenshot of your folder structure, I can try to recreate your issue and probably suggest a solution.
Hi there,
I'm fairly new in working with composer, but I'm experiencing issues. After some stackoverflow searches I tried some of te solutions, however, none of them did work for me. I Have the following error:
Fatal error: Class 'Freeby\Basic\Navigator' not found in index.php on line *.
So I took a look at my index. It contains the following code:
index.php
namespace Freeby;
use \Freeby\Basic\Navigator as Navigator;
Navigator::execute();
The line where the error occurs is the last one, Navigator::execute();. So I went to take a look at this class found in the folder Basic. Navigator.php
namespace Freeby\Basic;
class Navigator
{
public static function execute()
{
}
}
So, I have my namespace there. It should be recognized. However, it does not. So I went on to check my composer.json.
{
"require": {
"mikecao/flight": "^1.3"
},
"autoload": {
"psr-4": {
"Freeby\\Basic\\": "Basic/"
}
}
}
And I think this one is correct. However I'm not that sure. To be sure I'll include my structure here too. Maybe it's a path issue? If yes, why? I Couldn't find it.
---- Basic
- Navigator.php
---- Vendor
- autoload.php
-- composer
---- composer.json
I think you need to load the composer autoloader first in index.php
<?php
namespace Freeby;
require __DIR__."/vendor/autoload.php";
use \Freeby\Basic\Navigator as Navigator;
Navigator::execute();
I previously had a pretty simple autoload script working nicely, but as I've noticed that Doctrine2 is using Composer for this, I thought it might be nice to streamline everything. Unfortunately, Composer does not seem to be working as I understood it to.
Here is the relevant part of my composer.json
"autoload": {
"psr-0": {
"": "models/",
"Catalog2\\Config": "class/"
}
}
Note that the "": "models/" line used by Doctrine2 has been working just fine. After I ran composer update , the bottom part of my vendor/composer/autoload_namespaces.php looks like so:
'Doctrine\\Common\\' => array($vendorDir . '/doctrine/common/lib'),
'Catalog2\\Config' => array($baseDir . '/class'),
'' => array($baseDir . '/models'),
So far so good, I think. In my routes.php file (basically a front-controller) I have the following:
<?php
use Catalog2\Config;
//autoload classes
require_once __DIR__.'/vendor/autoload.php';
try {
$router = new Router;
} catch(Exception $e ) {
echo "<strong>Can't create router object</strong><br/>";
}
Here Catalog2\Config\Router should be calling my class/Router.php, which begins as follows:
<?php
namespace Catalog2\Config;
class Router {
protected $resource; //what are we manipulating? A product? An order?
protected $action; //what are we doing with that resource?
When I go to the page I get this:
Fatal error: Class 'Router' not found in /home/tom/Code/productCatalog2/routes.php on line 14
What is going wrong here? I repeat that Doctrine2 was able to autoload my model code from /models, so why aren't my changes working?
According to PSR-0 the namespace prefix will be included to the path.
So the complete filename for your class must be:
class/Catalog2/Config/Router.php
Meanwhile PSR-4 would behave like you expected: it will just match the namespace prefix and will not append it additionally to the given path.
References:
https://getcomposer.org/doc/04-schema.md#autoload
PS: you probably want the namespace prefix to be "Catalog2\\Config\\" (see the trailing slash)
I'm currently working on a Symfony2 project with Phpspec and I'm having problems to extend a Spec class described in a different namespace.
In my project, for instance, I'm having the following class described in spec/Acme/Model/Foo/FooSpec.php :
namespace spec\Acme\Model\Foo;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
abstract class FooSpec extends ObjectBehavior{
//some code here
}
And I have another class in spec/Acme/Model/Bar/BarSpec.php extending FooSpec :
namespace spec\Acme\Model\Bar;
use spec\Acme\Model\Foo\FooSpec;
class BarSpec extends FooSpec{
//some code here
}
When I try to run phpspec, I have the following error :
PHP Fatal error: Class 'spec\Acme\Model\Foo\FooSpec' not found in /home/user/Projects/Acme/spec/Acme/Model/Bar/BarSpec.php on line 9
The only way I found to make it work was to add the following line in spec/Acme/Model/Bar/BarSpec.php:
include('./spec/Acme/Model/Foo/FooSpec.php');
I don't know why I have to include this specific file to make it run, especially when the other classes (like PhpSpec\ObjectBehavior) are correctly found.
Do you have any idea why is this happening?
Edit:
As suggested by #Phil and #Sheikh Heera in the comments, I tried to set up an autoload to register my spec namespace but it's not working neither. Here is what I tried so far :
require_once getcwd() . '/vendor/composer/ClassLoader.php';
$loader = new \Composer\Autoload\ClassLoader();
// register classes with namespaces
$loader->add('spec', getcwd().'/spec');
// activate the autoloader
$loader->register();
I also tried to modify the file vendor/composer/autoload_namespaces.php to add this :
return array(
//some code here
'spec' => array(getcwd() . '/spec'),
//some more code
);
But still the same error. I also tried with 'spec' => array(getcwd()) or $loader->add('spec', getcwd()); just to see what will happen and this time I get a Cannot redeclare class on another spec class.
My php version is PHP 5.4.9-4ubuntu2.4 (cli).
Thank you in advance for your help.
A more generic solution is to put the spec directory into the autoload-dev configuration of your composer.json:
"autoload-dev": {
"psr-0": {
"spec\\":""
}
},
This way, composer will generate the namespace also for the specs, which should be on the root of the repository:
return array(
'spec\\' => array($baseDir . '/'),
...
);
Finally, what I actually did to "solve" my "problem", as suggested by Phil and Sheikh Heera in the comments, is to autoload my namespace so it is recognized by spec.
Here is what I did to make it work :
I added the following line to the file `vendor/composer/autoload_namespaces.php` :
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
//some code
'spec\\Acme\\Behavior' => array(getcwd()),
//some code
);
Then I created the following folder structure :
│── spec
│ └── Acme
│ └── Behavior
│ └── FooBehavior.php
I declared the namespace in `spec/Acme/Behavior/FooBehavior.php` this way :
namespace spec\Acme\Behavior;
And used it in `spec/Acme/Model/Bar/BarSpec.php` as following :
use spec\Acme\Behavior\FooBehavior;
class BarSpec extends FooBehavior{
//...
}
I know this is not the best practice because everytime someone will be working on this project it will have to reproduce manually the first step. So if you have any better idea, please feel free to comment or post an answer.