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
+ Applications\
+Accounting-system\
-SF_index.php
-SF_stock.php
-...more files.
+market-researcher\
-SF_index.php
-.....more files.
+ data \
- Class1.class.php
+ Acc\
Class2.class.php
+ KsaMarketResearsher\
Class3.class.php


• 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 and aap\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 or include ...

Examples

1. \contact.php

namespace aap;

2. \Applications\Accounting-system\stock.php

namespace aap;


In order of most to least relevant.

1) There are lots of frameworks for PHP websites / apps and you should consider using one. People have written lots of code that you can take advantage of and won't have to write for yourself. Some of it may even be good.

2) I would not store classes in a folder called 'data', because classes aren't data. I'd use 'classes' or 'lib' or 'include' or something else that better conveys what the folder contains.

3) Some commonly used classes (and interfaces) I suspect you may simply want to just include automatically, instead of having the overhead of parsing out where they are from their name with an autoloader. Consider having a second folder for these classes so you can do something like:

<?
foreach (glob('require\*.php') as $file) require$file;


for the ones which are used in many places.

4) Having to have 'require_once' at the start of each file is a bit icky - but it depends how big your project is before you worry about fixing it. If you use a framework the problem goes away - do whatever the framework documentation says.

If you want to clean it up with in-house code I would (and there may be other ways?) start by decoupling your request endpoints from the files that respond to the requests with a 'router' file, and have that do the autoloading.

That's perhaps a little vague, so here's an example:

Project structure:

htdocs\
boot.php
Application\
index.php
Accounting\
index.php
stock.php
Site\
index.php
signin.php
signup.php
Accounting\
index.php
stock.php
Include\
something.class.php
somethingelse.class.php
Acc\
another.class.php
Require\
exceptions.php
interfaces.php


Each of the files in 'Site\' looks like this:

<?
define('REQUEST_ENDPOINT', __FILE__);
include '\boot.php';


boot.php looks something like this:

<?
foreach (glob('Require\*.php') as $file) require$file;

/* anything else you want to do all the time
- access control for example */

// do something slightly smarter here
$file = str_replace('\\Site\\', '\\Application\\', REQUEST_ENDPOINT); include$file;


Files in 'Application\' look however they do at present, less any common code you can combine into the router (or something it includes before including your page).