-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-Autoloading.php
More file actions
108 lines (83 loc) · 3.62 KB
/
13-Autoloading.php
File metadata and controls
108 lines (83 loc) · 3.62 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/* Autolading in PHP */
// Autoloading may refer to different things, but generally it is how you load your classes.
// If you just use "require" (even in ONE big file) this still not autoloading,
// as ALL your files have to be required at once.
/* PHP standard autolading */
// Simple autolading built-into PHP
spl_autoload_register(function($class) {
// In this case all class files would be located in the folder "classes".
require_once "classes/{$class}.php";
});
/* Autoloading with Composer - Dependency Manager for PHP */
// Instead of creating your own autoloader, you can just use Composer,
// which give you many more options when working with other projects.
// Composer has many autoloading methods, but PSR-4 is highly recommended if your follow the PSR-2 standard.
// Read more: https://www.php-fig.org/psr/psr-4/
// Read more: https://www.php-fig.org/psr/psr-2/
// Composer is: "Composer is a tool for dependency management in PHP.
// It allows you to declare the libraries your project depends on and it will manage (install/update) them for you."
// Read more here: https://getcomposer.org/doc/00-intro.md
// ! Please note, that Composer require you to use a terminal to use it properly. !
/* Example autoloading with Composer */
// Install composer from terminal
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
/* Composer classmap Autoload */
//Composer also requires a "composer.json"-file in the root of your project like so.
{
"autoload": {
"classmap": [
"app/models"
]
}
// Require other dependencies yo your project
"require": {}
}
// Run in your terminal (CMD on Windows, Linux or Mac terminal)
composer self-update // only when Composer has not been used in a while
composer dump-autoload -o // Composer will generate optimized autoload files (fastest autoloading) which will create the folder "vendor" if it does not exist.
// After installation, you can use Composers autoload file in your project
require_once _DIR_ . '/vendor/autoload.php';
// Example class (located in the folder "app/models/") will now load automatically.
class User
{
public function __contruct()
{
echo 'Start';
}
}
// Returns "Start"
$user = new User();
/* Composer PSR-4 autoload */
//Composer also requires a "composer.json"-file in the root of your project like so.
{
"autoload": {
"psr-4": {
// Namespace and folder location for your whole project
"App\\": "app"
}
}
// Require other dependencies yo your project
"require": {}
}
// Run in your terminal (CMD on Windows, Linux or Mac terminal)
composer self-update // only when Composer has not been used in a while
composer dump-autoload -o // Composer will generate optimized ("-o") autoload files (fastest autoloading) which will create the folder "vendor" if it does not exist.
// After installation, you can use Composers autoload file in your project
require_once _DIR_ . '/vendor/autoload.php';
// Example class (located in the folder "app/models/") will now load automatically with the correct namespace.
// Notice the "\" is backwards.The namespace should be like the folder structure.
Namespace App\Models;
class User
{
public function __contruct()
{
return 'Start';
}
}
// Returns "Start". "Use" in like "require" in this case.
use App\Models;
$user = new User();