-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoload_example.php
47 lines (44 loc) · 961 Bytes
/
autoload_example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
define('BASE_DIR', __DIR__);
// anon func
function func($class)
{
$fn = BASE_DIR
. '/'
. str_replace('\\', '/', $class)
. '.php';
require $fn;
}
// anon func
$func = function($class) {
$fn = BASE_DIR
. '/'
. str_replace('\\', '/', $class)
. '.php';
require $fn;
};
// class that defines __invoke()
$anon = new class () {
public function __invoke($class)
{
$fn = BASE_DIR
. '/'
. str_replace('\\', '/', $class)
. '.php';
require $fn;
}
};
spl_autoload_register('func');
spl_autoload_register($func);
spl_autoload_register($anon);
use Test\Entity\User;
use Test\Service\DoNothing;
$user[] = new User('Fred', 'Flintstone');
$user[] = new User('Wilma', 'Flintstone');
$nothing = new DoNothing($user[0]);
echo $nothing->getFullName();
// this is not allowed: it's marked "Protected"
//$user->firstName = 'Betty';
// Fatal Error: cannot use $this outside of class context
// echo $this->firstName;
var_dump($user);