PSR-4 Standard of Autoloader and Usage with Composer
I don't know that but have you ever heard php-fig.org ? This guys trying to be standard for people who set the PHP structure. So they are saying, if you are going to set a structure, do it according to standards to it will be a thing :) ı will explain to easiest PSR-4 Autoloading deal.
If you are new with classes namespaces can be meaningless for you. Why should i use it? Now projects that used a lot of classes, you should think the namespaces liek a nickname if you don't they will clash it.
Assume that my name is phpExample and there are 2 more phpExample. To not get confused the namespaces I am phpExample designed for group A ,other phpExample is designed for group B.
If it happens like that nobody mix phpExample if they have a nickname. That's why we are using namespaces. Now you may say what is about, actually I don't know either I just wanted to inform you :) Our subject is how can we upload automatically our classes.
Usually we do
We were using _autoload() function. now with PHP 7.2 it's not recommended any more. Now we are using spl_autoload_register()
function. How do we use it;
spl_autoload_register(function($class){
$file = __DIR__ . '/src/classes/' . strtolower($class) . '.php';
if (file_exists($file))
require $file;
});
But this not enough now. Actually it is not logical. I might have other classes in my other files. That's why we didn'T think this as a problem.This FIG they indicated a autoloading standard that they all PSR-4. According to them these things should be;
\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>
So classes should have a top-level namespace. Also it is knowns as vendor namespace. In addition it should have 1 or more subspacename. And also class name.
If we do operation according to logic of this guys :)
spl_autoload_register(function($class){
// top-level namespace
$prefix = 'phpExample\';
// home directory for namespace
$base_dir = __DIR__ . '/src/libraries/';
// If the class being called does not contain the prefix, move on to the next install.
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0){
return;
}
// remainder of class except prefix
$relative_class = substr($class, $len);
// combine them all to create directory path
$file = $base_dir . str_replace('\', '/', $relative_class) . '.php';
// If there is a class file, upload it
if (file_exists($file)){
require $file;
}
});
// Actually this class is in
// src/libraries/validators/testvalidator.php
$test = new phpExample\Validators\TestValidator();
// src/libraries/database/testdatabase.php
$db = new phpExample\Database\TestDatabase();
Usage with Composer
{
"autoload": {
"psr-4": {
"phpExample\": "src/libraries"
}
}
}
Here we indicated the top-level namespace and the array that includes namespace.If we run the update command of composer on the terminal, you will see a vendor folder that contains autoload.php in main array. Also there is a Composer folder. Here we do operations and we called the autoload.php in our index file. And we try to use our classes.
<?php
require __DIR__ . '/vendor/autoload.php';
$test = new phpExample\Validators\TestValidator();
$db = new phpExample\Database\TestDatabase();
Result ? Yes it is working. Congrats, we achieved to use first standard :)
Comments
Popular Articles
Popular Tags
- #directive
- #Function
- #Json
- #Class
- #SASS
- #Form
- #Redirect
- #API
- #Special Characters
- #Special
- #Random
- #Generating
- #
- #Text
- #Ajax
- #URL
- #Encrypting
- #React
- #Show
- #timeAgo
- #PSR-4
- #Hide
- #DDOS Protection
- #DDOS
- #cURL
- #Logic For Displaying Posts
- #Error
- #Key
- #General Error
- #1364 Field
- #Abbreviation
- #Blade
- #Version
- #QR
- #QR Code Generating
- #Array
- #Arrays with Key Values to String Statement
- #Short Tag
- #Activate
- #Real Time
- #Socket.io
- #301
- #Custom Directives
- #Iframe Detection
- #Date
- #Characters
- #Insert
- #Autoloader
- #Composer
- #Reading
There are no comments, make the firs comment