I'm new to PHP and I didn't know there is something called coding style standards. Now I'm trying to stick to PSR coding style standards and after reading PSR-4, which is about autoloading standards. I wanted to change my php project to follow the standard. I'm also trying to change my project to be OOP rather than current procedural style.
Now I'm building the project files structure and autoloading strategy. I'm done with it and need your opinion about it, should I go on, or I need to change something?
This is the SF_autoloader.php file that I need your review on. It is in the native habitat (copied from Eclipse and pasted here).
<?php
spl_autoload_register(function ($class) {
$namespacePrefix = "aap";
$baseDirectory = __DIR__;
$fileName = str_replace($namespacePrefix, $baseDirectory, $class);
require_once $fileName . ".class.php";
});
This is my project files structure:
(C:\xampp\htdocs\)
- SF_index.php
- SF_contact.php
- SF_profile.php
- SF_signup.php
- SF_signin.php
- SF_about.php
- adminpanel.php
+ Applications\
+Accounting-system\
-SF_index.php
-SF_stock.php
-...more files.
+market-researcher\
-SF_index.php
-.....more files.
+ data \
- SF_autoloader.php
- Class1.class.php
+ Acc\
Class2.class.php
+ KsaMarketResearsher\
Class3.class.php
This is my auto loading strategy:
- I'm going to put every class file in the
Data\
folder - I'm using
aap
as the namespacePrefix for everything in this project - I'm also going to have 2 subname spaces which are
aap\Acc
andaap\KsaMarketResearsher
Now all I need to do to autoload is to include SF_autoloader.php/*SF=side effect*/
in every other PHP file, except the class files since the PSR says:
A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both.
It also says:
"Side effects" include but are not limited to generating output, explicit use of
require
orinclude
...
Examples
\contact.php
namespace aap; require_once "data\\SF_autoloader.php";
\Applications\Accounting-system\stock.php
namespace aap; require_once "..\\..\\data\\SF_autoloader.php";
So, what do you think about my autoloading PHP file?