What about PHP components? Ok, let's create. Part 1. Working with headers.
Maksim N Epikhin (PIKHTA)
Posted on May 4, 2021
I do a lot of programming in PHP and every time something starts that doesn't reach the end, and so on. As a result of thinking, I realized that every time I decide to write my own system, framework or something else, I run into a problem. The problem is that I do everything around for the system to work, and not the system itself, or I write too dependent code. As a result, I decided that it was worth taking the time to write only components that will not depend on something personalized, but can depend on each other. This approach is also a bit weird, but still passing an interface to work is better than initializing another class inside a class. For example, for some component it will be necessary to use a database, but we do not know exactly what methods are there, so we operate with our own interface, where we ask to implement a class with such and such a method. Something we have gone from the topic to abstraction.
And so, I began to make my written projects in pieces. As a result, nothing efficient came out, as there were strong dependencies. I had to write again ... The first component that I decided to implement was working with headers. Why exactly he? Because most of the work is built around the communication between the server and the client. And here, just, you need to work with headers.
For all my components, I decided to adhere to the same logic of organizing the file structure: a folder for interfaces, a folder for exceptions, and the class file itself. The folder with interfaces must contain at least 1 interface for the class. There may be no exceptions in the folder with exceptions, because you don't always need to have personal exceptions.
src/
--- interfaces/
--- --- ClassInterface.php
--- exceptions/
--- --- ClassException.php
--- Class.php
Now let's briefly talk about the methods that are available in the class for working with headers.
- set(array $params)
- add(array $params)
- remove(string $key)
- removeAll()
- has(string $key)
- get(string $key)
- getAll()
I don’t think it’s necessary to give a clear explanation of the methods, but if you are interested, you can look at the GitHub repository. True, now I'm translating my work into English, so don't be too surprised.
And at the end of the part, I want to note that there will be even more classes in the future. I try not to clutter up the post with code, since you can all look at GitHub. You can also download the project from composer packagist.
Posted on May 4, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.