Autoloader for functions
There is no function auto-loader for functions. You have four realistic solutions:
Wrap all functions into namespacing classes (context appropriate). So let's say you have a function called
string_get_letters
. You could add that to a class calledStringFunctions
as a static function. So instead of callingstring_get_letters()
, you'd callStringFunctions::get_letters()
. You would then__autoload
those namespaced classes.Pre-load all functions. Since you're using classes, you shouldn't have that many functions, so just pre-load them.
Load functions prior to using them. In each file,
require_once
the function files that are going to be used in that file.Don't use functions in the first place. If you are developing OOP code (which it seems like you are anyway), there should be little to no need for functions at all. Everything you would need a function (or multiple) for, you could build in a OO manner and avoid the need for functions.
Personally, I'd suggest either 1, 2 or 4 depending on your exact need and the quality and size of your codebase...
How to use multiple autoload functions for composer?
I want to use vendor/autoload.php - for composer files, but only my autoload-my-func.php for classes with my custom namespace. (My_Custom_Namespace)
You should use composer's auto loader for both and define your custom namespace in your composer.json file. If you are stuck with a poorly designed legacy system, and you need something to bridge a gulf of stupidity until you can create a better solution, then you should take a look at the documentation for spl_autoload_register. Specifically, the line about "if there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined." When you include composers autoload.php, you eventually get a few calls to spl_autoload_register(), so simply register your custom autoloader after including composer's.
Composer/PSR - How to autoload functions?
You can autoload specific files by editing your composer.json
file like this:
"autoload": {
"files": ["src/helpers.php"]
}
(thanks Kint)
PHP autoloader class vs. procedural autoloader function?
You are comparing functions with methods. That's just syntactic sugar.
Unless you have a map-based autoloader or one that has a builtin dependency table you don't need any class-level attributes to keep track of things (or could resort to static or global vars otherwise). Runtime reconfigurability is not a real necessity in practice either.
You can make a procedural autoloader project-configurable with constants etc. Having constructor properties isn't that significant of a benefit for reuse with a method implementation. It only may look slightly nicer.
How to use PHP Autoloader in multiple files?
That is what you do when you want to autoload your classes:
function my_autoloader($class) {
include $root . 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');
If you want to load anything else you can write a new function that gets called where you put all your require_once
stuff.
This works:
class Autoloader {
static public function loadEverything() {
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
//Autoloading classes
spl_autoload_register(function ($class) {
$root . 'Model/' . $class . '.php';
});
//Manually load needed files
require_once $root . "/src/Model/Device.php";
require_once $root . "/src/Model/Employee.php";
require_once $root . "/src/Model/User.php";
require_once $root . "/src/Controller/Controller.php";
}
}
Autoloader::loadEverything();
If you are new to mvc, you can watch the videos from Codecourse on youtube. He has like 25 videos about a mvc application and he explains everything so nice.
autoload' functions in php?
Since php 5.3.0 you could do someting like:
class Funcs
{
public function __callStatic($name, $args) {
if (!function_exists($name)) {
require_once sprintf(
'funcs/%s.func.php', // generate the correct path here
$name
);
}
if (function_exists($name)) {
return call_user_func_array($name, $args);
}
else {
// throw some error
}
}
}
And then use it like (for example):
Funcs::helloworld();
Which would try to load a file in funcs/helloworld.func.php
and execute helloworld
after successfull loading.
This way you could ommit the repeated inline tests.
Autoload classes and functions from different files and directories
Simple manual solution : put your autoload
file in your project root and include it in your index file this will do the job.
but if you want to use htaccess
or php.ini
:
Place a file called .user.ini
into the document root and add the auto_prepend_file directive in there:
auto_prepend_file = /home/user/domain.com/init.php
The file must be inside PHP's include_path. So you must either set the file's directory to be in the include_path inside php.ini, or do it in the .htaccess with a php_value statement.
php_value include_path ".:/path/to/file_directory"
php_value auto_prepend_file "file.php
If you use the above method in .htaccess, be sure to copy the include_path from php.ini in and add the :/path_to/file_directory
so you don't lose any already needed includes.
Alternatively, just add :/path/to/file_directory to include_path directly in the php.ini
Update
If you cannot modify the include_path, you might try specifying a relative path to the auto_prepend_file. This should work since the file path sent is processed identically as if it was called with require():
php_value auto_prepend_file "./file.php"
Related Topics
PHP Substring Extraction. Get the String Before the First '/' or the Whole String
Error Logging, in a Smooth Way
Stop People Uploading Malicious PHP Files Via Forms
Assigning the Return Value of New by Reference Is Deprecated
Json_Decode Returns Json_Error_Syntax But Online Formatter Says the Json Is Ok
Find Total Number of Results in MySQL Query With Offset+Limit
Storing and Displaying Unicode String (हिन्दी) Using PHP and MySQL
JavaScript: How to Create Jsonp
PHP Fatal Error: Class 'Pdo' Not Found
Filter Values from an Array Similar to SQL Like '%Search%' Using PHP
PHP Read Sub-Directories and Loop Through Files How To
How to Clear Browser Cache With PHP
Access Controller Method from Another Controller in Laravel 5