autoload

Autoloading non-PSR0 libraries in Symfony 2.0.x

跟風遠走 提交于 2019-12-06 05:27:29
问题 The Symfony 2.0 Autoloader expects that the libraries it can handle follow the PSR0 or PEAR standard when auto-loading files. If you have an old library which does not follow any of these two standards (in my case, class files are named like name.class.php), how would you handle auto-loading of these libraries? In Symfony 2.1 this is easy as composer supports classmaps and can load this type of libraries, but how would you do it in Symfony 2.0.x ? 回答1: Inside app/autoload.php , create an

Doctrine, namespace and autoload entities

自古美人都是妖i 提交于 2019-12-06 03:48:38
I want to use Doctrine 2 in my project. I've some problems with it. I read the docs but probably I'm doing something wrong. I want to autoload entities classes. And method from the docs is not working. My bootstrap.php <?php require_once "vendor/autoload.php"; use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping\Driver; $paths = array("../Entities"); $isDevMode = false; $classLoader = new \Doctrine\Common\ClassLoader('Entities','../Entities'); $classLoader->register(); // the connection configuration $dbParams = array( 'driver' => 'pdo_mysql', 'user' => 'xxx',

Autoloading functions best practices

可紊 提交于 2019-12-06 01:22:43
When working on a PHP project that takes advantage of the OOP paradigm with PHP's __autoload() function, which of the following is considered the best practice for managing stand-alone functions: ( Examples provided are simplified for the sake of brevity ) tl;dr : How is stand-alone function loading commonly handled: pseudo-autoloading (via __callStatic magic for example) [ Option 1 ] abstract helper class grouped static methods [ Option 2 ] an alternative Also note, I've posted a related question regarding a parameter/reference issue with option 1 , which can be found here: __callStatic(),

How do I NOT autoload a class in php?

走远了吗. 提交于 2019-12-05 21:55:13
the autoloader at the beginning of my php code function __autoload($class_name) { include_once $class_name . '.class.php'; } is causing a call to new MongoClient(); to fail with the error Warning: include_once(MongoClient.class.php): failed to open stream How can I use the autoloader for my classes and still use the standard classes? Note: MongoDb has been installed with PECL and works fine with the autoloading function removed. mongo-1.3.0beta2 on php 5.4.9 __autoload() , if defined, is called each time you try to access a class that has not been imported manually using require_once() or

Can't pass class instance to constructor

给你一囗甜甜゛ 提交于 2019-12-05 19:20:25
I have a User eloquent model that takes in an instance of the UserMailer class in its constructor but I get this error Argument 1 passed to User::__construct() must be an instance of TrainerCompare\Mailers\UserMailer, none given, called in /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 631 and defined I understand the error but can't figure out what I have done wrong but I don't udnerstanding namespacing and composer class map vs psr0 autoloading very well. I have remembered to use composer dump-autoload so it is not that relevant folder structure composer

Check whether instance of a class exists, if not create an instance

回眸只為那壹抹淺笑 提交于 2019-12-05 17:42:56
问题 I was wondering if it's possible to create a function and pass it a class name. The function then checks if an instance of the class currently exists, if it does not it create an instance of the class. Furthermore if possible make that variable global and require it to be returned. I realize that returning may be the only option. function ($class_name) { // Check if Exists // __autoload will automatically include the file // If it does not create a variable where the say '$people = new people

PSR-4 autoloader Fatal error: Class not found

百般思念 提交于 2019-12-05 16:36:53
问题 I have my project structure like so: src/ ├─ Model/ └─ User.php My User.php file looks like this: <?php namespace Bix\Model; class User { And my composer.json autoloader is this: "autoload": { "psr-4": { "Bix\\": "src/" } } Finally my bootstrap.php is this: use Bix\Model\User; // PSR-4 Autoloader. require_once "vendor/autoload.php"; However if I try and create a new User() , I get the error Fatal error: Class 'User' not found in /var/www/public/api/v1/index.php on line 8 Looking at the

Import package or autoloading for PHP?

自古美人都是妖i 提交于 2019-12-05 13:08:21
What solution would you recommend for including files in a PHP project? There aren't manual calls of require/include functions - everything loads through autoload functions Package importing, when needed. Here is the package importing API: import('util.html.HTMLParser'); import('template.arras.*'); In this function declaration you can explode the string with dots (package hierarchy delimeter), looping through files in particular package (folder) to include just one of them or all of them if the asterisk symbol is found at the end of the string, e.g. ('template.arras.*'). One of the benefits I

Rails unable to autoload constant from file despite being defined in that file

我怕爱的太早我们不能终老 提交于 2019-12-05 10:25:26
问题 This is a tricky one to explain. I have a module in another module namespace like so: # app/models/points/calculator.rb module Points module Calculator def self.included(base) base.send(:include, CommonMethods) base.send(:include, "Points::Calculator::#{base}Methods".constantize) end end end So then in other classes all I need to do is: class User include Points::Calculator end I've specified this directory in application.rb to be autoloadable...(even though i think rails recurses through

Zend Framework 2: Composer\\Autoload\\includeFile is slow

别来无恙 提交于 2019-12-05 10:18:20
According to New Relic transaction tracer, sometimes Composer\Autoload\includeFile takes around 318 ms to load my project. I have dumped a classmap from composer, but still no difference it made. composer.json requires the following: "require": { "php": ">=5.3.3", "zendframework/zendframework": "2.4.*", "zendframework/zendservice-amazon": "2.*", "wisembly/elephant.io": "~3.0", "knplabs/github-api": "~1.2" } I only have one module in my ZF2 application. How can my ZF2 autoloading perform faster? We had the exact same issue while debugging in New Relic. Finally, we traced it to the filesystem,