spl-autoload-register

PHP namespacing and spl_autoload_register

谁都会走 提交于 2020-01-14 07:57:10
问题 I had spl_autoload_register working fine but then I decided to add some namespacing to bring in PSR2 compliance and can't seem to get it working. Directory strcuture: -index.php -classes/ -Class1.class.php -Class2.class.php -Class3.class.php Each class starts with: namespace Foo; Class ClassX { Index.php: <?php spl_autoload_register(function($class) { include 'classes/' . $class . '.class.php'; }); $myObj = new Class1(); echo $myObj->doSomething(); This products an error Fatal error: Class

PHP class not found when using namespace

安稳与你 提交于 2019-12-29 05:32:07
问题 I am new with this namespace thing. I have 2 classes(separate files) in my base directory, say class1.php and class2.php inside a directory src/ . class1.php namespace \src\utility\Timer; class Timer{ public static function somefunction(){ } } class2.php namespace \src\utility\Verification; use Timer; class Verification{ Timer::somefunction(); } When I execute class2.php , i get the Fatal error that PHP Fatal error: Class 'Timer' not found in path/to/class2.php at line *** I read somewhere on

How to use spl_autoload_register?

血红的双手。 提交于 2019-12-20 11:34:34
问题 class Manage { spl_autoload_register(function($class) { include $class . '.class.php'; }); } Say I have some code like the above. I chose to use the anonymous function method of loading classes, but how is this used? How exactly does it determine which '$class' to load? 回答1: You can't put the code there. You should add the SPL register after your class. If you wanted to register a function inside the Manage class you could do: class Manage { public static function autoload($class) { include

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

Autoloader for distributed PHP plugins that includes composer packages

本秂侑毒 提交于 2019-12-12 06:51:45
问题 I am working with a CMS that requires the user to upload extensions. The users do not have access to composer. So I will need to include the dependencies in the distribution itself. Would how I implement it work for autoloading all dependencies? Is this the way to go about this? Here's the simplified directory structure of the distributed extension. (The user is supposed to upload the contents of the upload directory): upload/Eg/ upload/Eg/autoload.php <-- autoloader to load the dependencies

Load a class with a different name than the one passed to the autoloader as argument

巧了我就是萌 提交于 2019-12-11 05:29:56
问题 basically, I have the following problem: I want to make use of PHP's new namespace features. Unfortunately, I'm running a PHP version (5.3.2) in which namespace-autoload-support for linux still seems buggy and does not work (PHP should be able to load the class file automatically by its namespace without having to use a custom autoloader, but that doesn't work). What I want to achieve is to write an autoloader that I can simply remove as soon as the php's namespace features work correctly

spl_autoload_register issue while loading class

淺唱寂寞╮ 提交于 2019-12-10 10:24:18
问题 So I already asked this question here earlier, but the solutions provided didn't work for me. Here's my setup: /mylib /Vendor/Module/MyClass.php /document_root index.php Here's my index.php <?php define('CLASSDIR', 'mylib'); define('BASEPATH', @realpath( dirname (__FILE__).'/../').'/'.CLASSDIR); spl_autoload_register(null, false); spl_autoload_extensions('.php'); function autoLoader($className){ $className = ltrim($className, '\\'); $fileName = ''; $namespace = ''; if ($lastNsPos = strrpos(

PHP __autoload() with namespace

不问归期 提交于 2019-12-08 11:42:38
问题 spl_autoload_register('Think\Think::autoload'); Under namespace Think\ I created the above register function,when I try to use a class that has not been included like class Storeage,php will surposely pass Storeage as the variable to function Think\Think::autoload,but it actually passed Think\Storeage as the variable,why it adds the extra Think\ to the autoload instead of just Storeage? Does that mean autoload will only search for classes which are declared under the same namespace where the

PHP classes with a namespace cannot be loaded via spl_autoload_register?

别来无恙 提交于 2019-12-07 12:24:17
问题 I have a problem in loading classes with spl_autoload_register when a namespace is implemented in a class. the class autoloader below, but I have no problem loading any class when no namespace is used, class autoloader { /** * Set the property. */ public $directory; public $recursive; /** * Receive the supplied data. * @string $directory * @array $recursive default: models */ public function __construct($directory, $recursive = array('search' => 'models') ) { # Store the data into the

Using the PHP spl_autoload_register() with Codeigniter

主宰稳场 提交于 2019-12-06 14:11:38
问题 Please how can I use spl_autoload_register() with Codeigniter? I need to do this because Im using Codeigniter with another framework which also uses autoload. I saw something here PHP spl_autoload_register but I dont know how to target the CodeIgniter autoload . Im new to OOP and Codeigniter. Thanks a lot! The above link has this: function autoload_services($class_name){ $file = 'services/' . $class_name. '.php'; if (file_exists($file)){ require_once($file); } } function autoload_vos($class