Autoloading Classes
Autoload
tree .
.
├── ArrayUtil
│ └── ArrayUtil.php
├── Hello.php
└── index.php
1 directory, 3 files
<?php
namespace ArrayUtil;
class ArrayUtil
{
public static function min($array): float
{
return \min($array);
}
}
<?php
class Hello
{
public static function display($name)
{
return "Hello $name";
}
}
<?php
// require 'Hello.php';
// require 'ArrayUtil/ArrayUtil.php';
function __autoload($className) {
$className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
$filename = __DIR__ . DIRECTORY_SEPARATOR . $className . '.php';
if (is_readable($filename)) {
require_once($filename);
echo "\n######### $filename\n";
}
}
use ArrayUtil\ArrayUtil;
var_dump(Hello::display("Fulano")); //=> string(12) "Hello Fulano"
var_dump(ArrayUtil::min([1, 2, 3])); //=> float(1)
Tips:
- PHP can load class files automatically on demand (No explicit require statements are needed);
- The file name must match the case of the terminating class name (each class in a separate file);
- The directory name must match the case of the namespace names;
__autoload()
has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged.
REFERENCES
SPL Autoload
tree .
.
├── ArrayUtil
│ └── ArrayUtil.php
├── Hello.php
└── index.php
1 directory, 3 files
<?php
namespace ArrayUtil;
class ArrayUtil
{
public static function min($array): float
{
return \min($array);
}
}
<?php
class Hello
{
public static function display($name)
{
return "Hello $name";
}
}
<?php
// require 'Hello.php';
// require 'ArrayUtil/ArrayUtil.php';
spl_autoload_register(function ($className) {
$className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
$filename = __DIR__ . DIRECTORY_SEPARATOR . $className . '.php';
if (is_readable($filename)) {
require_once($filename);
}
});
use ArrayUtil\ArrayUtil;
var_dump(Hello::display("Fulano")); //=> string(12) "Hello Fulano"
var_dump(ArrayUtil::min([1, 2, 3])); //=> float(1)
REFERENCES
Autoload File
tree . -I assets
.
├── ArrayUtil
│ ├── Arithmetic
│ │ └── Arithmetic.php
│ ├── ArrayUtil.php
│ └── Stats
│ └── Stats.php
├── autoload.php
└── index.php
3 directories, 5 files
<?php
namespace ArrayUtil;
use ArrayUtil\Arithmetic\Arithmetic;
use ArrayUtil\Stats\Stats;
class ArrayUtil
{
use Arithmetic, Stats;
}
<?php
function MyAutoload($className)
{
$extension = spl_autoload_extensions();
$className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
$filename = __DIR__ . DIRECTORY_SEPARATOR . $className . $extension;
if (is_readable($filename)) {
require_once($filename);
} else {
die("Not Found Class ($filename)");
}
}
spl_autoload_extensions('.php');
spl_autoload_register('MyAutoload');
<?php
require_once 'autoload.php';
use ArrayUtil\ArrayUtil as ar;
$array = [1, 2, 3, 4];
var_dump(ar::sum($array)); //=> float(10)
var_dump(ar::min($array)); //=> float(1)
Composer
PSR-4
$ composer init
$ composer install
$ tree . -I 'vendor|assets'
.
├── ArrayUtil
│ ├── Arithmetic
│ │ └── Arithmetic.php
│ ├── ArrayUtil.php
│ └── Stats
│ └── Stats.php
├── composer.json
└── index.php
3 directories, 5 files
{
"name": "lucachaves/composer-psr-4",
"require": {},
"autoload": {
"psr-4": {
"ArrayUtil\\": "ArrayUtil/"
}
}
}
<?php
require __DIR__ . '/vendor/autoload.php';
use ArrayUtil\ArrayUtil as ar;
$array = [1, 2, 3, 4];
var_dump(ar::sum($array)); //=> float(10)
var_dump(ar::min($array)); //=> float(1)
$ composer dump-autoload -o
REFERENCES
- PSR4 Autoloading Your PHP Files Using Composer
- Composer autoloading
- composer.json (autoload)
- PHP Composer: The Autoloader
PSR-0
$ tree . -I 'vendor|assets'
.
├── ArrayUtil
│ ├── Arithmetic
│ │ └── Arithmetic.php
│ ├── ArrayUtil.php
│ └── Stats
│ └── Stats.php
├── composer.json
└── index.php
3 directories, 5 files
{
"name": "lucachaves/composer-psr-4",
"require": {},
"autoload": {
"psr-0": {
"ArrayUtil\\": "ArrayUtil/"
}
}
}
Classmap
$ tree . -I 'vendor|assets'
.
├── ArrayUtil
│ ├── Arithmetic
│ │ └── Arithmetic.php
│ ├── ArrayUtil.php
│ └── Stats
│ └── Stats.php
├── composer.json
└── index.php
3 directories, 5 files
{
"name": "lucachaves/composer-classmap",
"require": {},
"autoload": {
"classmap": [
"ArrayUtil/"
]
}
}