PHP PSR-4 Autoloading Made Easy
Why Stack Overflow recommends it
Stack Overflow users link it when PHP developers are drowning in require_once calls or confused about how PSR-4 and Composer autoloading work.
Recommended by 1 people across 1 different months, 2015 to 2018.
What it is
A 17-minute Codecourse tutorial from 2014 showing how PSR-4 autoloading works in PHP and how to set it up with Composer. It walks through namespaces and the composer.json autoload configuration so classes load without manual require statements.
What people said
Recommends Melvin M. · 2016-02 · Stack Overflow · on “How to work a file on another file without using require() or require_once() functions in php?”
Try using composer to autoload you classes To get started with composer visit: [LINK] You can read the documentation here: [LINK] If you are more of a visual learner you can see this [this video] to get you started.
Cites Alexander Solonik · 2015-10 · Stack Overflow · on “Invisible autoloading in PhilePhP”
@deceze , i was more interested in what mechanism was being used to autoload .. i found this [this video] , .. i beleive it is PSR 4 and composer . i am a newbie to server side so be gentle :)
Cites JosefPP · 2016-01 · Stack Overflow · on “required file not working but required file exist”
Why don't you just use autoloading? [this video] Maybe you just need to define the absolute path to the project root. Put this in a separated file and require it on your public index.php define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT'] . '/your_project_root' ); then in your public index.php just require_once'../app/the_config_file.php'; now when you dynamically require something just do it like: require_once ROOT_PATH . '/path_to_the_file.php';
Cites Morrolan · 2016-01 · Stack Overflow · on “composer PSR4 autoloading issue”
I am trying to implement PSR-4 autoloading in my project. It is an existing application I have inherited that doesn't currently use OOP (it's all scripted), but I am hoping to refactor over time to use OOP, and PSR-4 is crucial for me as it will end up being a large project. At this present time we do not rely on any external libraries (old application entirely written in-house), so composer is ONLY being used for autoloading at this moment in time, but will use third-party packages eventually such as phpunit. My app structure on my server looks like this: /var/www/appurl/ │ composer.json │ ├───htdocs │ ├───AppName │ │ ├───admin │ │ │ authenticated.php │ │ │ init.php │ │ │ login.php │ │ │ │
Cites icecub · 2018-10 · Stack Overflow · on “What is php's include order?”
Ye `__DIR__` should work fine. And you include the initial config by hard coding `__DIR__` in your script. I personally work with composer [LINK] so I have PSR-4 autoloading [this video] available to me. So I don't have to worry about this.
Cites Imnotapotato · 2018-10 · Stack Overflow · on “Using namespaces instead of requiring each class separately”
This is my init.php file required by the index.php file: <?php /** * * Initialization file: Will require all components we need. * **/ /* Set session */ session_start(); // Start session /* Helpers */ require_once 'helpers/Sanitize.php'; // Sanatizing (escaping) strings require_once 'helpers/Token.php'; // Generate tokens require_once 'helpers/Hash.php'; // Hash class require_once 'helpers/Input.php'; // Inputs manipulations require_once 'helpers/Validation.php'; // Validation class require_once 'helpers/Redirect.php'; // Redirect helper require_once 'helpers/Session.php'; // Session manipulations require_once 'helpers/Cookie.php'; // Cookies manipulations /* Core */ require_once 'core/Confi