dave
dave

Reputation: 11985

Can you enforce architectural layer dependencies in PHP?

Let's say I have a simple, multi-layer architecture for an application. Typical layers might be (starting at the bottom and working our way up): core / utils, entity / data, business logic, and UI. Dependencies between classes should only be in the "down" direction, eg. from the UI to the business logic, and not the other way around. How would I go about enforcing this in PHP? Can IDEs (such as PHPStorm) do it? Can composer do it?

If I was working in Java, a language I'm more familiar with, I would typically be using Eclipse and Maven. Each layer would be a separate project and would produce a JAR file. Layers would depend on each other via the classpath. For example, the business logic layer (or project) would have the util and entity JAR files available and not the UI. If I mistakenly created a dependency in the "up" direction, the IDE would flag it as an issue and the code would not compile.

I haven't been able to find a way to achieve the same in PHP. Any ideas?

Upvotes: 3

Views: 287

Answers (2)

user3692317
user3692317

Reputation: 21

The thing is to enforce the separation of what each layer should know and do, what I call responsibility. Each layer should have responsibility for on what it is expected to do without any one layer knowing how the others do. My simple architecture has:

1) Interface with HTML INPUT elements that get/set its content to the database trough the second layer classes.

1.1) More complex custom made interface PHP class elements like VECTOR, SELECT and GRID that allow to select one database record from a list presented on this elements.

2) PHP classes that define the data attributes and SQL queries. This level implements the basic FIND, INSERT, ALTER and DELETE class methods to send/receive data using SQL queries to/from the next level database access. I this level, usually, the classes are named to the database table and the attributes to the fields of the databases tables that they will have access.

3) A database class with the particular database brand commands like MySQL and Oracle for example. This level could be done by a class with all the commands for all databases or a single class for each database. If your system works with only one database You could let the upper level (2) class know and resolve which database class to call. But if You are going to need to access more than one database on the same application or need to change often the database, it is better to have just one class with all commands on it. If the class in this level access only one database it is named to the database.

The way I designed, the several layers communicate on both directions by getting setting data.

[]s

Upvotes: 0

alexander.polomodov
alexander.polomodov

Reputation: 5534

Dependencies between classes should only be in the "down" direction, eg. from the UI to the business logic, and not the other way around. How would I go about enforcing this in PHP? Can IDEs (such as PHPStorm) do it? Can composer do it?

Generally speaking, the answer is no (or I haven't been able to find out ho to do this)... but it is possible to enforcing appropriate layering by good engineering practices.

For example:

  • system architect can supply the whole team with well designed component diagram and composite structure diagram
  • system architect can separate the whole project on sub projects and create dependency map for whole project. We using composer and its composer.json. For example, our entity data layer is just such subproject with full set of Mappers over our SOA services:)
  • system architect can enforce code review in every team and between teams not only for common code ownership but also to support appropriate layering
  • it's usually helpful to use template engine for UI (It adds rigor)
  • each member of the team should understand basic concepts of reducing the complexity of systems (It can be useful to show them popular and successful engineering systems like OSI network model)

Generally speaking, everything in the hands of the team. That's why this freedom of PHP allows you to make a great thing and a complete waste. And it would be nice to finish this answer with paraphrasing the well-known phrase:

With great freedom comes great responsibility:)

Upvotes: 1

Related Questions