3

What about PHP components? Ok, let's create. Part 1. Working with headers.

 3 years ago
source link: https://dev.to/maximepihin/what-about-php-components-ok-let-s-create-part-1-working-with-headers-4hgj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Cover image for What about PHP components? Ok, let's create. Part 1. Working with headers.

What about PHP components? Ok, let's create. Part 1. Working with headers.

May 4

・2 min read

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
Enter fullscreen modeExit fullscreen mode

Now let's briefly talk about the methods that are available in the class for working with headers.

  1. set(array $params)
  2. add(array $params)
  3. remove(string $key)
  4. removeAll()
  5. has(string $key)
  6. get(string $key)
  7. 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.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK