Answer the question
In order to leave comments, you need to log in
How do you organize modularity in your PHP projects?
Hey Habr.
The plans here are to make a game and knowing that this is a huge amount of code, I want to immediately solve the problem with modularity. At the moment I use the following scheme:
1) Each class is a separate file. For example, Foo.php (namespace Game; class Foo), etc.
2) I connect all the necessary classes through include_once to each of the classes. Example:
//index.php
include_once './session.php';
include_once './database.php';
include_once './include/Map.php';
include_once './include/Town.php';
include_once './include/Game.php';
$game = new \Andromeda\Game();
//Game.php
namespace Andromeda;
include_once './../database.php';
include_once './../session.php';
include_once './Map.php';
include_once './Town.php';
class Game
{
private $db = '';
Answer the question
In order to leave comments, you need to log in
1. If pure php, then Composer + dependency manager and PSR
2 standards. Either Yii/Yii2 framework, Symfony 2 (there is no lack of frameworks) + Composer (Yii 2 supports composer, but it is under development. Symfony 2 supports composer)
Instead of all this pack of inclusions, insert somewhere like this
function class_loader($className){
include_once $className.'.php';
}
spl_autoload_register('class_loader');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question