E
E
Eugene Obrezkov2013-06-15 23:32:55
PHP
Eugene Obrezkov, 2013-06-15 23:32:55

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 = '';

But with this approach, you won’t turn around, because. error 500 constantly falls. Advise how it is better to structure your project?
PS C PHP is not very familiar. Often everything was written in pure JS. Now I understand that zhs is not enough and if you want it, you don’t want it, but PHP needs to be twisted.
PPS Maybe even some framework can be used?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
avalak, 2013-06-16
@ghaiklor

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)

E
egorinsk, 2013-06-16
@egorinsk

Read about autoloading classes (autoloading)

K
Konstantin Kiyashko, 2013-12-22
@konst20

Instead of all this pack of inclusions, insert somewhere like this

function class_loader($className){
      include_once $className.'.php';
}

spl_autoload_register('class_loader');

The files will be included as the objects are created.
The file name must match the class name.
The design does not conflict with other autoloaders, verified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question