Autoloading Classes

Autoload

tree .
.
├── ArrayUtil
│   └── ArrayUtil.php
├── Hello.php
└── index.php

1 directory, 3 files

ArrayUtil/ArrayUtil.php

<?php

namespace ArrayUtil;

class ArrayUtil
{
  public static function min($array): float
  {
    return \min($array);
  }
}

Hello.php

<?php

class Hello
{

  public static function display($name)
  {
    return "Hello $name";
  }
}

index.php

<?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:

REFERENCES

SPL Autoload

tree .
.
├── ArrayUtil
│   └── ArrayUtil.php
├── Hello.php
└── index.php

1 directory, 3 files

ArrayUtil/ArrayUtil.php

<?php

namespace ArrayUtil;

class ArrayUtil
{
  public static function min($array): float
  {
    return \min($array);
  }
}

Hello.php

<?php

class Hello
{

  public static function display($name)
  {
    return "Hello $name";
  }
}

index.php

<?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

ArrayUtil Object

ArrayUtil/ArrayUtil.php

<?php
namespace ArrayUtil;

use ArrayUtil\Arithmetic\Arithmetic;
use ArrayUtil\Stats\Stats;

class ArrayUtil
{
  use Arithmetic, Stats;
}

autoload.php

<?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');

index.php

<?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

composer.json

{
  "name": "lucachaves/composer-psr-4",
  "require": {},
  "autoload": {
    "psr-4": {
      "ArrayUtil\\": "ArrayUtil/"
    }
  }
}

ArrayUtil Object

index.php

<?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

PSR-0

$ tree . -I 'vendor|assets'
.
├── ArrayUtil
│   ├── Arithmetic
│   │   └── Arithmetic.php
│   ├── ArrayUtil.php
│   └── Stats
│       └── Stats.php
├── composer.json
└── index.php

3 directories, 5 files

composer.json

{
  "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

composer.json

{
  "name": "lucachaves/composer-classmap",
  "require": {},
  "autoload": {
    "classmap": [
      "ArrayUtil/"
    ]
  }
}

References