autoload

Replacement for PHP's __autoload function?

荒凉一梦 提交于 2019-12-19 02:28:48
问题 I have read about dynamically loading your class files when needed in a function like this: function __autoload($className) { include("classes/$className.class.php"); } $obj = new DB(); Which will automatically load DB.class.php when you make a new instance of that class, but I also read in a few articles that it is bad to use this as it's a global function and any libraries that you bring into your project that have an __autoload() function will mess it up. So does anyone know of a solution?

Instantiating class by string using PHP 5.3 namespaces

∥☆過路亽.° 提交于 2019-12-18 10:54:39
问题 I can't get around an issue instantiating a new class by using a string variable and PHP 5.3. namespaces. For example, this works; $class = 'Reflection'; $object = new $class(); However, this does not; $class = '\Application\Log\MyClass'; $object = new $class(); A fatal error gets thrown stating the class cannot be found. However it obviously can be instantiated if using the FQN i.e.; $object = new \Application\Log\MyClass; I've found this to be aparrent on PHP 5.3.2-1 but not not in later

PHP Autoloading in Namespaces

我与影子孤独终老i 提交于 2019-12-17 18:07:42
问题 I have had a slight problem with autoloading in my namespace. As shown on the PHP manual here: http://us.php.net/manual/en/language.namespaces.rules.php you should be able to autoload namespace functions with a full qualified name e.g. \glue\common\is_email(). Thing is I have a function spl_autoload_register(array($import, "load")); within the initial namespace but whenever I try and call \glue\common\is_email() from the initial namespace it will not pass that autoload function but when using

PHP - most lightweight psr-0 compliant autoloader

允我心安 提交于 2019-12-17 09:33:55
问题 I have a tiny application that i need an autoloader for. I could easily use the symfony2 class loader but it seems like overkill. Is there a stable extremely lightweight psr-0 autloader out there? 回答1: You ask extremely lightweight, let's do so ;) Timothy Boronczyk wrote a nice minimal SPL autoloader : http://zaemis.blogspot.fr/2012/05/writing-minimal-psr-0-autoloader.html I condensed the code like this: function autoload1( $class ) { preg_match('/^(.+)?([^\\\\]+)$/U', ltrim( $class, '\\' ),

Where should I add autoload cookies in my Emacs Lisp package? Is there a definitive guide?

ぃ、小莉子 提交于 2019-12-14 01:25:23
问题 I maintain a somewhat popular Emacs package (ido-ubiquitous), and I would like to add the ;;;###autoload comments to my package so that it does not need to be loaded until its mode is activated. I attempted this a while ago with the same package, and it resulted in a number of cryptic errors that only occurred when the package was autoloaded, so I removed all the autoload stuff and the problems went away. I'd like to try again, but only if I can find definitive documentation on how to do it

Unable to AutoLoad Class using Composer

爱⌒轻易说出口 提交于 2019-12-14 00:29:34
问题 I have a project structure that looks like: app/ app/models/ app/controllers/ app/views/ public/ vendor/ composer.json Inside of app/controllers/IndexController.php, I have: require '../vendor/autoload.php'; use MyApp\Models\Test; class IndexController { public function __construct() { $t = new Test(); // can't be found } } Here's my composer.json: { "require": { "aws/aws-sdk-php": "*", }, "autoload": { "psr-0": { "MyApp": "app/" } } } After updating composer.json, I run composer.phar update

PHP: __autoload function doesn't ever called

左心房为你撑大大i 提交于 2019-12-13 14:50:46
问题 So, I have xampp. I tested this code on ZendServer, result the same. <?php error_reporting(E_ALL); define ("ABS_PATH", 'C:\xampp\htdocs\oopHotLine\\'); function __autoload($class_name) { echo 'gg'; require_once (ABS_PATH.'classes\\'.$class_name.'.php'); } $process=new Main('1'); ?> after php.exe -a index.php i have this: Interactive mode enabled Fatal error: Class 'Main' not found in C:\xampp\htdocs\oopHotLine\index.php on line 10 [Finished] so, it doesn shows 'gg' output. If i manually do _

Is __autoload() called for parent classes of autoloaded classes?

淺唱寂寞╮ 提交于 2019-12-13 14:24:46
问题 In main.php , autoload is added and a new object is created: function __autoload($class) { require_once($class . '.php'); } ... $t = new Triangle($side1, $side2, $side3); In Triangle.php : class Triangle extends Shape {...} Shape.php is an abstract class: abstract class Shape { abstract protected function get_area(); abstract protected function get_perimeter(); } I can see that __autoload function calls Triangle.php , but does it call Shape.php at the same time? 回答1: No (not at the exact same

PHP autoload classes from different directories

孤街浪徒 提交于 2019-12-13 09:45:56
问题 I have found this code that autoloads all classes within single directory, and it works pretty good. I would like to be able to extend it a bit to load classes from different paths (directories). Here is the code: define('PATH', realpath(dirname(__file__)) . '/classes') . '/'; define('DS', DIRECTORY_SEPARATOR); class Autoloader { private static $__loader; private function __construct() { spl_autoload_register(array($this, 'autoLoad')); } public static function init() { if (self::$__loader ==

Mustache_Autoloader missing with Composer

Deadly 提交于 2019-12-13 05:17:00
问题 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(