Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Directory structure to write OOP PHP with PSR-4 autoloader - steps how to setup PSR-4

In case some of you do not know how to do it, let these few simple steps help you a bit..

In order to write OOP PHP you should consider using something called "PSR-4 autoloader" and composer, more information on this link: http://www.php-fig.org/psr/psr-4/
This little toy will help us to work with namespaces and provide us convenient way to include classes (scripts) whenever and wherever we need (using the correct way).
This is the usual project structure i personally use in my projects (if i use raw PHP) and i believe it will work just fine for you too.

If some of you disagree with me, if some of you believe this is not something people should use and if some of you believe there is a better way to organize your project, PLEASE let me know so i can consider it and adapt the change (but of course, make sure you provide explanation to back up your facts/suggestion).



Let's begin

First, install composer, which can be downloaded here: https://getcomposer.org/ (it's fairly simple, few "next-next" clicks)

Then, the very first thing i do, is create a project root directory and "src" folder inside of that directory to store my source files:

[Image: 6AppzXo.png]

The next thing i do is, create composer.json file in the project root directory and fill with following content:

[Image: N4kQELL.png]

These few lines of code say that we are going to use PSR-4 autoloader.
As you can see, i wrote something like: "MyApp\\": "src/" inside of that psr-4 object, that means, whenever i write "MyApp" in my scripts as a namespace, it will look into "src" folder directly. Then you just chain your folders as your namespaces. You can add as many of these as you need (but you usually need just one to represent the "src" folder in a more convenient way).
For example, say you have folder structure like: "src/Core/Utilities", your namespace will be like:
PHP Code

<?php 
namespace MyApp\Core\Utilities; 
instead of
PHP Code

<?php 
namespace src\Core\Utilities; 

Now, in order to make this work, we need to run "composer install" from command line so it generates the autoloader for us.
It will create "vendor" folder along with some content in it, which is required for the psr-4 autoloader to work correctly.

[Image: TJnEorl.png]

[Image: 0VaqZzi.png]

Now, the last thing you need to do is to include THE "autoload.php" file in your application startup script (main entry point), which is located in the auto-created "vendor" folder.
Usually, startup script is your index.php file.
(as you can see, i for the testing purpose "required" the SomeClass and instantiated its object. for example you can create instance of your main application class and run the main method to start the application)

[Image: nS7PE70.png]

In case you dont have a startup script (one main entry point for your application), say you need to support namespaces somewhere else, just require the "autoload.php" file there and you can use your classes like a boss.

Source code: https://ufile.io/2c2e3

Long story short:
1.) download and install composer
2.) create composer.json with the content i said and "src" folder to keep your source files
3.) run "composer install" to generate autoloader
4.) require just one file, the "/vendor/autoload.php" file and use your classes like a boss :D



I might not be the best tutorial writer, i mean, i do suck hard at it lol But i hope you get the point and idea how to use it.

Feel free to ask any question, i'll be more than glad to answer (if i know the answer :P)
Gopher


Possibly Related Threads...
Thread / Author Replies Views Last Post
by SnoctrizMar 1, 2024 06:00 PM 7 239 Mar 1, 2024 06:00 PM
Last Post: Hexicidal
[PHP] by OneandonlySunny™Apr 22, 2020 10:54 PM 9 213 Apr 22, 2020 10:54 PM
Last Post: OneandonlySunny™
[Javascript]  by VirtualFrostJan 14, 2020 05:31 PM 3 141 Jan 14, 2020 05:31 PM
Last Post: VirtualFrost