📜 ⬆️ ⬇️

Step-by-step algorithm for creating a PHP site architecture

Disclaimer : this text made me write the almost complete lack of imputed materials on this topic in Russian. This is badly taught in universities, the PHP tutorials and the official manual are silent about this, although this is the most important point in developing a program - the creation of an architecture. Bad architecture can kill your project, so it will never see the light. Good architecture, even with bad code, and which of the newbies writes good code, can work wonders.

The development of the architecture should begin at the moment when the idea “I want a site of such and such” or someone has put in the idea of ​​“we need a site of such and such” has occurred to you. Unfortunately, most developers immediately begin writing code, and not even the one that is needed.

Before writing the code, arm yourself with a piece of paper with a pen, wordpad, Word, writer or even your code editor and follow a couple of steps.

Step 1. Setting the main task


The architecture is developed from top to bottom. What does it mean? This means that you first think through the most important subsystems before proceeding with servicing them or more technical ones.
')
Many programmers start developing websites from routers, connecting to the database, and handling errors, which leads to the need to rewrite their code in the future or a rich insertion into the code of crutches as soon as they stumble upon what they could not foresee.

It is impossible to foresee everything, and for this it is necessary to develop a system from top to bottom. The presence of working high-level classes almost automatically determines all the necessary requirements for the service staff.

This is not the only method, but I know from experience that the most optimal for those who do not feel like fish in water among design patterns or even do not know about them at all.

So, you need a blog, an online store, a dental clinic website.

Decide that your site is the most important thing for which everything is started.

For a blog, these are obviously posts. For the store - the goods. For the site clinics - services.

We can already write some code from which the development of the site will begin:

class  { } 

or class goods {}, or class services {}

This is your main subsystem. Immediately you can write exactly what it will manage.

 class  { } 

or class goods {}, or class service {}

Step 2. Determine what your system can do


We have created the main subsystem "records". The next step: determine what it will be able to do, what we want to teach our site.

Obviously, she should be able to create entries, delete entries and get entries.

Forget that you write the code of the site, forget about the users, url-addresses, page layout. Work with bare data: strings, numbers, arrays, objects.

Write the code:

 class  { public function _() {} punlic function _() {} public function _() {} } 


Step 3. Understand what we are dealing with.


What is a record? What is a service? What is a product? For you, as a programmer, this is just a collection of data. In the third step, determine what kind of aggregate.

Do not seek to foresee everything that will be needed sometime in the future. Take the most minimal set. To do this, we are building the architecture of the application so that it can be easily extended in the future.

Obviously, the record is the title and text of the record. Service and product - the name, description and price.

A computer is not a very clever creation, compared to a person, you cannot tell him: “but show me the record, the one where it’s written about the green kitten”, so he always needs some kind of unique code so that he is among the many records learned exactly the one you want.

This is either a sequence number (id), unique for each entry, or some unique string value.

So our entry is the entry identifier (id), title, text.

We don't need anything else.

Step 4. Determining the required knowledge


Now for each action of the system we need to understand what the system needs to know in order to perform this action.

For the action "create-record" you need to know the title and text of the record. The internal identifier is needed only by the computer, so let him invent it himself.

For the actions “delete-record” and “get-record”, you only need to know the record identifier that was assigned to it by the computer when it was created.

 class  { public function _($, $) {} punlic function _($) {} public function _($) {} } 

What happened is called the interface of your class / module / subsystem.

Step 5. Determination of results


Also, as we learned the necessary knowledge to perform a particular action, we need to understand how these actions will end.

If the computer has succeeded, then


If the computer does not work, then


For all situations when something doesn’t work, the behavior of the program should be the same in all modules / classes / subsystems of your system. PHP provides only two options: trigger_error and throw new Exception. You can come up with your own, but do not reinvent the bikes until you have learned how to ride existing ones. throw new Exception is the best option.

Read the section " exceptions " in the official manual.

Step 6. From the beginning again


In the first step, in addition to creating a records management system, we also created a class for the record itself: class record {}

For it you need to do the same thing as for the main one, the same steps from 1 to 5.

If you did everything right, then you should have something similar.

 class  { public function _() {} public function _() {} punlic function _() {} public function _($_) {} public function _($_) {} } 


Step 7. Write the code


Now, when everything is created, you can start coding. If you are familiar with PHP no problems you will not have.

How much time was spent? Just a little bit, and still feel in the future all the benefits of what happened.

What you will end up hiding under the letter M in the terrible abbreviation MVC.

Conclusion


I did not take into account many nuances, they will be enough for ten of the same articles, but the basis for your development is already there. These are the very basics and in any case it is better than interfering with PHP and HTML in one file or generating a bunch of functions as you write code.

CM sorted out. As for V and C. This is a topic for other articles. Therefore, in brief. If you are smart enough, then you will not need other articles.

C is the place where the system responds to user actions. That's where you need to think what the user will do. The user wanted to create a blog entry, filled out the creation form, and you have everything ready to execute his command. Wanted to read the record and again you fulfill his request in one line. There the main task (step 1) is to understand what the user wants.

V - learn any template maker: Smarty or Twig. There is a lot of controversy about whether to use template engines or not, many convincing arguments on both sides. The truth is that it’s unforgivable for a web developer not to be able to work with template engines. Therefore, master them, and then refuse, if you do not like it.

As noted above, in Russian there is almost no decent literature on this topic.
The only book that I can advise to read: Steve McConnell "Perfect Code".

If someone knows other materials, throw a link at me. I am pleased to read and will recommend to beginners.

Source: https://habr.com/ru/post/247495/


All Articles