How to register namespaces (with PHP 5.3) in the Symfony 1.4 for the autoloader class feature (like the Symfony 2.0)?
You can use Autoloader from Symfony2 in Symfony 1.4 framework.
1. Copy Symfony2 classloaders to vendor directory of your Symfony 1.4 sandbox project:
SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/UniversalClassLoader.php
SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php
2. Modify your SF_ROOT_DIR/config/ProjectConfiguration.class.php file as follows:
require_once dirname(__FILE__) . '/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
require_once dirname(__FILE__) . '/../lib/autoload/sfClassLoader.class.php';
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration {
public function setup() {
$this->namespacesClassLoader();
$this->enablePlugins('sfDoctrinePlugin');
}
public function namespacesClassLoader() {
if (extension_loaded('apc')) {
$loader = new ApcUniversalClassLoader('S2A');
} else {
$loader = new UniversalClassLoader();
}
$loader->registerNamespaces(array(
'Pohon' => __DIR__ . '/../lib/vendor/Pohon/src'));
$loader->register();
}
}
3. Register desired namespaces:
eg. I want to load class:
Pohon\Tools\String\Utils\Slugify.
Filename must be:
SF_ROOT_DIR/lib/vendor/Pohon/src/Pohon/Tools/String/Utils/Slugify.php
and registered namespace as follows:
Pohon => SF_ROOT_DIR/lib/vendor/Pohon/src
You can use Composer and it's very easy. Just install it on your machine (you probably have already since it's 2015 now) and run in your project folder:
composer init
You can then install all the packages you want with composer and include just this line in your ProjectConfiguration.class.php:
require_once __DIR__.'/../vendor/autoload.php';
Note that paths may differ if you changed the default Symfony1.4 directory structure.
Symfony uses the spl_autoload_register() function to register its own autoloader (sfAutoload).
You can register your own handler in the initialize() function of your Project/Application/Plugin. (whichever applies).
This is, for example, also what the Swift_Mailer plugin does: it registers its own autoloader when needed.
Related
I'm trying to use Doctrine MongoDB ODM 2.0 beta on a project with the Yii2 framework, with composer version 1.8.4 and PHP 7.2, but I keep getting the error Fatal error: Uncaught Error: Call to a member function add() on boolean where the code runs $loader->add('Documents', __DIR__);
bootstrap.php file (in DIR/bootstrap.php):
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$config = new Configuration();
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setDefaultDB('fsa');
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents'));
$dm = DocumentManager::create(null, $config);
I already tried looking at How to properly Autoload Doctrine ODM annotations? and Laravel & Couchdb-ODM - The annotation "#Doctrine\ODM\CouchDB\Mapping\Annotations\Document" does not exist, or could not be auto-loaded and a host of other threads I can't quite recall for help, but I couldn't figure out a solution.
I also tried commenting out the lines below
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
and ran composer dump-autoload and on command line it returned Generated autoload files containing 544 classes, but then I got the problem
[Semantical Error] The annotation "#Doctrine\ODM\MongoDB\Mapping\Annotations\Document" in class Documents\Message does not exist, or could not be auto-loaded.
So the annotations are not auto-loading, and I have no idea how to fix that.
In the model I have:
<?php
namespace Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use \Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
/** #ODM\Document */
class Message
{
/** #ODM\Id */
private $id;
/** #ODM\Field(type="int") */
private $sender_id;
...
I also posted a thread on github at https://github.com/doctrine/mongodb-odm/issues/1976. One commenter stated that "By default, the composer autoload file returns the autoloader in question, which seems to not be the case for you." How can I fix that? The only information I can find online is to put (inside composer.json) the lines:
"autoload": {
"psr-4": {
"Class\\": "src/"
}
},
but then what class should I be loading?
I'm very confused and being pretty new to all these tools (mongodb, yii2, etc.) doesn't help at all. I'm not sure what other information would be helpful else I would post it.
Thanks in advance.
So turns out that the problem (as was mentioned in https://github.com/doctrine/mongodb-odm/issues/1976) was that autoload.php was required twice - once in bootstrap.php and once in web/index.php (of the framework). After the require line in index.php was removed, everything worked fine.
I got difficult to include composer autoload in Class file, it not working on require_once('../vendor/autoload.php');
require_once('phpmailer/PHPMailerAutoload.php');
require_once('../vendor/autoload.php');
class Test {
function X()
{ ... }
}
What is the proper way to load multiple include files in a class?
If you (correctly) use composer you need do add only the vendor autoload file. Then add the other dependency via composer vendor library or add custom path (composer do the rest for you).
As example, more simply:
start in an empty directory
launch the command:
php composer.phar init
Add the dependency of the library in the composer.json files (if you don't add it in the init process) with the command (suggested by the packagist site)
composer require phpmailer/phpmailer
Then your class should be like:
require_once('../vendor/autoload.php');
class Test {
function X()
{ ... }
}
Hope this help
I think you want something like this
class Loader
{
public function __construct()
{
require_once('phpmailer/PHPMailerAutoload.php');
require_once('../vendor/autoload.php');
}
}
$loader = new Loader();
just add some function as you want
tell me if this help you ... goodluck
The error:
\nFatal error: Class undefined: DI in /home/runeko/www/index.php on line 7
My folder structure:
- core
- DI.php
- index.php
index.php:
<?php
set_include_path( __DIR__ );
spl_autoload_extensions('.php');
spl_autoload_register();
use \core;
$di = new DI();
//i tested too: $di = \core\DI();
DI.php
<?php
namespace \core;
class DI {
}
HHVM version:
root#akai:/var/log/hhvm# hhvm --version
HipHop VM 3.0.0-dev+2014.04.03 (rel)
Compiler: heads/master-0-g6398683df312015ebc40066dfe4b9242cc933a60
Repo schema: b4303360e484eee1f582365b9994eee150d450b0
is hhvm bugged? thanks.
The solution:
*This feature isn't implemented yet* ->
https://github.com/facebook/hhvm/issues/2308
http://docs.hhvm.com/manual/en/language.types.object.php
According to the official documentation, the object instantiation is not changed as in PHP, you need to use new keyword
use \core;
$di = new DI();
Hello guys i have problem with autoloading my class with composer. On Linux all work perfect, but now my boss change env and set Windows. All this work on linux but windows show newbie fatal error:
Fatal error: Class 'AbstractController' not found in
D:\xampp\htdocs\ikacFw\frontController.php on line 7
Common to see my composer.json and stucture for better picture on problem.
Stucture is :
frontController.php
-- vendor
----- Doctrine
----- Ikac
--------- Components
---------- Mvc
------------- Controller
Am trying to load all data from vendor directory.
Composer.json
{
"autoload": {
"psr-0": {
"vendor": ""
}
}
}
Also new component i add manual. Like this :
$loader = require_once 'vendor/autoload.php';
$loader->add('vendor', "Ikac");
Okay next when i try to call :
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller;
$a = new AbstractController();
I get error "not found".
My class AbstractController contain defined namespace but dont work again. Like test i do this:
<?php
//vendor/Ikac/Mvc/Controller/AbstractController.php
namespace Ikac\Mvc\Controller;
class AbstractController {
function __construct() {
echo __CLASS__;
}
}
?>
I do from cmd composer dump-autoload, install, but dont work. All this perfect work on linux but here wont. Any idea how to fix this or where i do mistake.
Thanks guys!
SLOVED:
{
"autoload": {
"psr-0": {
"": "vendor/"
}
}
}
Well you should do
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller\AbstractController;
$a = new AbstractController();
Your autoloading declaration is wrong.
You will NEVER ever need to include the vendor folder in any autoloading. The vendor folder will contain the autoloading for both all dependencies, and - if configured - for your own classes as well.
You can use Composer to create autoloading for your own classes. Just include the correct info. But from your current info I cannot deduct what would be correct.
I added to my project FOSUserBundle, on localhost it's works fine. But on web server I get
Fatal error: Class 'FOS\UserBundle\FOSUserBundle' not found in
/home/zone24/domains/zone24.linuxpl.info/public_html/worldclock/app/AppKernel.php on line 22
I can't cache:clear because I get this same message.
My autoload.php
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
The line from AppKernel.php who make mistake
new FOS\UserBundle\FOSUserBundle(),
Folderfriendsofsymfony in /vendor has 775 permisions
Are you using APC ? If yes, restart apache to clear its cache.
If that does not help, you can always force the autoloader to register a specific namespace with $loader->add(), but you should not have to do that. FOS works fine for me without adding that.