V
V
Vlad Sharikov2014-04-27 02:42:46
IT education
Vlad Sharikov, 2014-04-27 02:42:46

Should I write my own php framework in order to improve my OOP knowledge and learn the MVC pattern?

In general, everything is said in the title of the post :)
What is the reason for the question?
Since childhood, I have been interested in programming (or rather, at first it was a simple HTML page with photos with bananas, pictures right in the text that knocked down the whole format, etc. :)). Now I am studying HTML, CSS, JS (and with it jQuery), PHP (and many other things in general, but I only listed the technologies related to the question). This question is about PHP. I believe that I know him at some level - just above the plinth. Of course, this is a relative indicator, but at least now, reading questions on stackoverflow.com on these topics, some of them make me smile (and if the topic of the question is a misunderstanding of how to compare 2 variable values ​​​​and signed in CodeIgniter or Zend Framework, then rzhach), and not my questions make someone smile.
There are several projects created by me (or rather, a series of sites with similar topics, similar target audience) - these are not huge portals, but simple sites with low traffic (again, a very target audience), but they fulfill their task. The websites are built using pure PHP. The engine was written from scratch (not a framework, not a CMS) by me.
Now I have to support these projects and I often come across the fact that everything is somehow not very convenient. The point is that all processing, SQL queries, screen outputs, logic are all in one heap. I would like to share it somehow. Here we come to the point.
Frameworks were created just for this - in order to make life easier for the programmer (built-in functions, modules, framework plugins) + in order to separate screen output from logic and from working with the database. Correctly? Correctly.
I believe that in order to understand something, you need to do it yourself, and then also teach someone what you yourself understood. I'm thinking about creating something like this myself (no, no one is going to argue with Zend or Yii, God forbid): first, to learn the MVC approach; secondly, to expand knowledge in the field of OOP; Thirdly, get some experience. Create such a framework and, for example, write a portfolio on it (after all, it would be time already).
So the question is: is it worth creating your own bike or is it "just" "stupid" to take some ready-made solution and deal with it?
I want to hear your opinion and, maybe, your furious stories from childhood with HTML pages :)
PS: I hope you did not fall asleep while reading it :)
PPS: I tried to present the question not as "purely holy"

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
romeo7, 2014-05-18
@romeo7

Reading your question, I remembered myself 5 years ago :) At that time, my background consisted of a dozen sites on various CMS and one startup, which, of course, did not take off. By that time, I had been hatching for a long time a plan for the rehabilitation of one frozen project (sports portal), which I had developed with my comrades back in my student years. Initially, there was no task to write your own engine, but ... it all started with the development of a template engine with a syntax a la CMS MODx. I read a lot about the fact that this implementation stands out from the background of other competitors, in fact being the visiting card of the latter. In fact, it turned out that this is just a syntactic abstraction over Smarty, with all the consequences in terms of performance. For example, my implementation has alternative native php template support
for those who can't stand syntactic abstractions (Smarty, Twing, Fenom and others.) because of their low performance and other religious reasons.
As time went on, the code base grew. From Singleton pattern to DI (Dependency Injection), to Service Locator. A lot of things have been cut out for the sake of existing solutions. Over the past 2 years, with the help of Composer, there have been many times more ready-made solutions, and several for the implementation of a specific task. For example, from the latter, its own file manager (manipulation with the file system) was replaced with the Flysystem library , because the latter, among other things, can "run" to the clouds. Cool ;) The only thing in my implementation was the ability to search for a regexp pattern, I had to write an abstraction.
Here is an anti-example. A fairly popular data validation library is Respect Validation . It was required to implement the internationalization of messages, it was not possible to display the hierarchy of errors thrown during validation in one large array, and I also wanted to return, for example, only the first error (first) or the last (last). I had to fork, because it is simply impossible to abstract.
If you still decide to write your own framework, then rely on existing solutions. Choose for yourself one or two popular frameworks and study how this or that functionality is implemented in them, at least visually, thanks to the documentation in bulk. Start with the MVC model. Look at how actions are implemented, namely access to them, filtering at the entrance and exit in the action. For example, I am not a supporter of the implementation of pseudo-annotations, as in Symfony, because there is no support for native annotations in PHP yet, and everything else is a pitiful semblance through slow Reflections, even if everything is cached. So the author of the AOP paradigm for PHP understands all this, but still continues to develop his Go project! . The implementation is certainly interesting, but I will get by with the event model (Observer or PubSub).
Take a look at how routing works, because it is fundamental to the implementation of SOAP and REST. For example, my solution was influenced by Lavarel Routing regarding the use of groups. Nowadays, due to the prevalence of mobile platforms, REST is increasingly used not only based on url, but also based on custom headers (X-<pragma name>).
Pay attention to how errors/exceptions are handled. For example, Yii has long been criticized for the fact that there is no way to correctly identify this or that exception. It is better for each individual task (for example, FileManager) to have its own Exception class, which is inherited from the base one:

class Exception extends BaseException
{
    const FILE_EXISTS = 'File exists: {path}';

    public function __construct(
        $level = self::ERROR,
        $msg = null,
        array $dataReplace = null,
        \Exception $handler = null
    ) {
        return parent::__construct($level, $msg, $dataReplace, $handler);
    }
}

Then, in the BaseException base class, you can easily implement logging (for example, use Monolog ) and beautiful visual output in debug mode (for example, Whoops ).
It is advisable to immediately rid yourself of the habit of making "fat" controllers / actions. Various implementations of validation, data filtering, as well as setting default values ​​​​at the model level can help you with this. Notice the rules method. Then in your controller there will be only a method for sending already processed data to the view.
As for ORM and DBAL (synonyms: DAO and Query Builder), then in this case it is definitely not worth reinventing your own "bicycle". To write, if possible, a single interface for various relational and non-relational solutions (DBMS, Full-text search systems / indexers (Sphinx, Elasticsearch)) is more than a non-trivial task. I took AR (ORM) and Query Builder Yii2 as a basis in my framework. Yes, there is no modularity in Yii, and therefore everything is quite dependent on each other, but if you want, you can.
Feel this moment. You dissect an almost finished (note Yii2 is still in beta), one of the most outstanding solutions at the moment and thus understand all the intricacies, along the way you are active in correcting errors.
Learn to write unit tests. Many mistakes will come to the surface, and then your sleep will be stronger.
You probably could have noticed for yourself on the same stackoverflow or other resources, quite trivial questions are asked about frameworks. And now, while I am writing this tremendous answer to you, in the section "Similar Questions" there is such a question "How to correctly implement authorization with sessions ...There is no elementary discipline to independence. And I can even guess why. There are more frameworks, there are better frameworks. Programmer Friendly, and not a scary beast for the elite, as it once was. True, some of them are still unfriendly grinning to this day;) One way or another, if you want to ask a question from the series how to compare two variables in JQuery, then you should think about whether you need all this.
In no case do not need to convince yourself that your instrument will take off. It is not unique, and in the end it will most likely consist of many ready-made libraries (note in my case 60 versus 40% of the vendor code. Considering the significance, then in this case, the score will not be in my favor). Unfortunately, I can’t find a link to an English-language article where the author complains about the complete rejection of frameworks in favor ofpackagist . Even if there is no doubt about the uniqueness of your solution, this does not guarantee you anything. Competent promotion is necessary - a lot of articles on thematic resources, as well as participation in conferences. For example, PHPDaemon , although it started first, is still outperformed by ReactPHP , and all that was needed was to pay attention to writing documentation. The author of PHPDaemon, Vasily Zorin, once asked what his project hopes for from React, pointed out that the latter uses his ideas. Of course, it's sad for our compatriot, but his project smacks of outright selfishness.
Make your instrument primarily for yourself, and perhaps someday it will become interesting to someone else. One way or another, you will get invaluable experience. The main thing is to try to bring this matter to the end. The hallmark of our craft profession is patience. Here is a test of this wonderful human quality for you :) By the way, so that interest does not fade away, it is worth applying your achievements, if not in production, then at least a small project for experiments.
I noticed for myself that while I was developing the tool, I gained much more experience than in the previous two jobs. But this is subjective. You can get a job from the very beginning in a place where there is a wonderful responsive team and no less interesting projects / startup, and not "pull a template on Wordpress". I think that even a CMS-snika is a road to nowhere, and Kipelov has nothing to do with it :) The sooner, the sooner;)
I, like sWinDos , also find it funny to look at my sources a year ago :)) You know such a thing as " Thesaurus"? In the interpretation of information theory, this is an exponential growth of knowledge/experience up to a certain limit, after which the effectiveness of the knowledge/experience gained noticeably drops.
PS I specifically did not touch on the moral and financial side of the issue. Opensource or earnings? The eternal search for free time between family, work and leisure. I look here someone has already checked in :) If you are still a student and are not burdened with anything, then go for it. It is quite possible that after graduating from the university or even earlier, you will come out confident with such a stocky middle :) By the way, in the first organization with which I started my professional career they preached procedural programming, because OOP was not understood properly by anyone.

S
svd71, 2014-04-27
@svd71

you, apparently, while writing maolo thought.
1. If you set a goal to make money on this, then this is a rather worthless means of earning money: it is unlikely that you will be able to create a "masterpiece", if someone dares to use it, who will support it? For the end client, who needs all this to make money, there is absolutely no need to be interested in picking the code or looking for the reason for the hack. He will try to find specialists and products in proportion to the amount he is willing to pay. Therefore, then it’s better to strain your efforts towards a specific product that other people are competitively pushing. Or most competitively push. But sitting on two chairs is usually not enough seat.
2. If all the same, in order to "understand how it still spins," then of course a good solution. But it would be much more profitable to poke around on some kind of oDEsk for money (albeit not big ones).
3. In one language (although php is a scripting language) it is not possible to "expand OOP knowledge". You need at least a couple more, to dig a torus. To understand the advantages and disadvantages of designs, differences and functionality.

C
Crash, 2014-04-27
@Bandicoot

It’s not worth it, it’s better to take a few popular frameworks and pick them up properly

S
sWinDos, 2014-04-27
@sWinDos

I had the same thoughts about 6 years ago. And yet, he began to make his own framework working with php for only about a year at that time. The matter did not reach its logical conclusion, it was done: working with the database, processing pages, access control, something like a media library, pagination and other very basics.
Now it is difficult to look at your code of that time without shame and fear, but at that time knowledge was well advanced. Only time I then had a carriage.
The author, if you have time for this, and you don’t feel sorry for him, then I would advise you to do not just a framework, but separate module files for everyday needs. And improve them over time.

N
Nazar Mokrinsky, 2014-04-27
@nazarpc

From my own experience, I can advise you to work with something ready-made, and when it comes to understanding that something does not suit you here and there, when there are enough arguments in favor of writing your own, start writing or fork an existing project. You probably don’t realize now how much of a huge piece of work it is to make a framework that is reliable, convenient, fast and extensible enough. It will most likely be a waste of a lot of time. If you first work with ready-made ones, there will be an understanding of how others solve problems, and ideas will appear on how to do better.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question