autoload

Using Composer's Autoload

孤人 提交于 2019-11-26 18:52:54
问题 I have been looking around the net with no luck on this issue. I am using composer's autoload with this code in my composer.json : "autoload": { "psr-0": {"AppName": "src/"} } But I need to autoload at a higher level than the vendor folder. Doing something like this does not work: "autoload": { "psr-0": {"AppName": "../src/"} } Does anyone know a fix or how I can do this? 回答1: Every package should be responsible for autoloading itself, what are you trying to achieve with autoloading classes

How do I use PHP namespaces with autoload?

吃可爱长大的小学妹 提交于 2019-11-26 17:12:00
I get this error when I try to use autoload and namespaces: Fatal error: Class 'Class1' not found in /usr/local/www/apache22/data/public/php5.3/test.php on line 10 Can anyone tell me what I am doing wrong? Here is my code: Class1.php: <?php namespace Person\Barnes\David { class Class1 { public function __construct() { echo __CLASS__; } } } ?> test.php: <?php function __autoload($class) { require $class . '.php'; } use Person\Barnes\David; $class = new Class1(); ?> Class1 is not in the global scope. See below for a working example: <?php function __autoload($class) { $parts = explode('\\',

Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?

大憨熊 提交于 2019-11-26 15:12:06
I understand that you can use either a PSR standard to locate files, or tell composer a directory to scan for classes. The documentation recommends using the PSR-4 standard. There is also an option for composer to create an optimized autoloader, which basically generates a full classmap . So why should one use PSR-4 at all if the best way to load is with a classmap? It makes sense to me to keep the directory structure, since that is a good way to organize anyway. However, it seems like the logical option would be to use PSR-4 loading on development machines, and then classmap for the

What is Autoloading; How do you use spl_autoload, __autoload and spl_autoload_register?

我们两清 提交于 2019-11-26 11:59:12
I am learning advanced PHP standards and trying to implement new and useful methods. Earlier I was using __autoload just to escape including multiple files on each page, but recently I have seen a tip on __autoload manual spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future. but I really can't figure out how to implement spl_autoload and spl_autoload_register Brownbay spl_autoload_register() allows you to register multiple functions (or static methods from your own

How to I use Composer to autoload classes from outside the vendor?

只愿长相守 提交于 2019-11-26 11:07:08
问题 I use psr-4 autoloader from composer: \"autoload\": { \"psr-4\": { \"DG\\\\Munchkin\\\\\": \"src/DG/Munch/\" } } This loads classes from /var/www/html/xxx/vendor/yyy/src/DG/Munch But how can I load classes from /var/www/html/xxx/ ? I wrote my own autoloader, but when I require vendor/autoload.php (composer autoload) and my autoloader, it won\'t work until I create instance of a class in my own autoloader. 回答1: The src directory would be in your project root. Its on the same level as vendor

Autoloading classes in PHPUnit using Composer and autoload.php

落爺英雄遲暮 提交于 2019-11-26 10:21:26
问题 I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test. I would like to have all my classes autoloaded into each unit test without having to use include or require at the top of my test but this is proving to be difficult! This is what my directory structure looks like (a trailing / slash indicates a directory, not a file): composer.json composer.lock composer.phar lib/ returning.php tests/ returningTest.php vendor/

Rails 5: Load lib files in production

无人久伴 提交于 2019-11-26 06:13:08
问题 I\'ve upgraded one of my apps from Rails 4.2.6 to Rails 5.0.0. The Upgrade Guide says, that the Autoload feature is now disabled in production by default. Now I always get an error on my production server since I load all lib files with autoload in the application.rb file. module MyApp class Application < Rails::Application config.autoload_paths += %W( lib/ ) end end For now, I\'ve set the config.enable_dependency_loading to true but I wonder if there is a better solution to this. There must

Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 04:18:44
问题 I understand that you can use either a PSR standard to locate files, or tell composer a directory to scan for classes. The documentation recommends using the PSR-4 standard. There is also an option for composer to create an optimized autoloader, which basically generates a full classmap. So why should one use PSR-4 at all if the best way to load is with a classmap? It makes sense to me to keep the directory structure, since that is a good way to organize anyway. However, it seems like the

What is Autoloading; How do you use spl_autoload, __autoload and spl_autoload_register?

北城以北 提交于 2019-11-26 02:27:02
问题 I am learning advanced PHP standards and trying to implement new and useful methods. Earlier I was using __autoload just to escape including multiple files on each page, but recently I have seen a tip on __autoload manual spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future. but I really can\'t figure out how to implement spl_autoload and spl_autoload_register 回答1

Best way to load module/class from lib folder in Rails 3?

徘徊边缘 提交于 2019-11-25 23:29:30
问题 Since the latest Rails 3 release is not auto-loading modules and classes from lib anymore, what would be the best way to load them? From github: A few changes were done in this commit: Do not autoload code in *lib* for applications (now you need to explicitly require them). This makes an application behave closer to an engine (code in lib is still autoloaded for plugins); 回答1: As of Rails 2.3.9, there is a setting in config/application.rb in which you can specify directories that contain