Just don’t say that you weren’t like that or my view of learning

Hello. I would like to tell my opinion about modern programming training. The article will talk about PHP, a little about frameworks and a little about bikes. But I think that applies to any programming language. Let's get started.







Remember your first PHP code?



No, not this one:







<? echo "Hello world"; //       index.php :) ?>
      
      





And this one:







 <? include "db.php"; if ($_GET['id'] > 0){ mysql_query("SELECT * FROM `posts` WHERE id = {$_GET['id']}"); }else{ mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC"); } ?>
      
      





Over time, we grew up, we had an understanding of OOP (ha ha), and we began to produce something similar to this:







 <?php require "db.php"; if ($_GET['id'] > 0){ $db->query("SELECT * FROM `posts` WHERE id = {$_GET['id']}"); }else{ $db->query("SELECT * FROM `posts` ORDER BY `id` DESC"); } ?>
      
      





Then we heard somewhere that mysql_ * cannot be used and everyone uses mysqli_ *, as this is more cool. We rewrote our code to use mysqli. After some time, we learned about PDO.







In parallel, we worked with global variables and hundred-line classes in each method. Are you familiar with the User class, which was able to register a user, authenticate (yeah, authorize of course, we don’t know the difference yet!), Recover a password? I know. Our User class could edit any user, because we had a super method for this:







 // PSR? ,  . class User { public function changeUserProfile($userId, array $userData){ global $db; //    $db->query("UPDATE `user` SET ...."); } public function login($userId){ } public function logout(){ //     <a href="/logout.php"></a> :) } }
      
      





And I also had classes Db, Post and Comment.







We are familiar with SQL injection, XSS attacks and:







  DROP TABLE `posts`
      
      





SOLID, PSR, MVC and patterns are not just words



We went this way ourselves, learned about SOLID, PSR, MVC. For us, these are not just words, but an urgent need, especially after the terrible legacy and heaps of code, each of the classes of which is written in its own style. We stepped on thousands of rakes, wrote as many bicycles, and as a result, began to write more / less adequate code.







We no longer use global and superglobal variables in our classes, for SQL we use prepared queries and do not put SQL in controllers (we also no longer have UserModel :: login ()). We implement the necessary objects using DI, but do not create them inside the class. We drive Request / Response, not echo "hello world" and we know why we are doing this. We test our code not only using a browser, but also write synthetic tests. What is the price of this? I have a childhood with a computer, dozens of sleepless nights, as many fakaps and a broken database.







And I also understood early why $ _POST in models is bad.







Weren't you that kind of person? I was.







So it was before. But not now



Until now, I can’t understand whether the new mod for PHP development courses is evil or not. On the one hand, people are offered to learn how to write their code correctly in a short time, but on the other hand, people don’t understand why this is the correct way to write. A whole layer of knowledge is lost, the base, which then will have to be quickly made up.







Now they offer dozens of courses. In just a few hours, you will learn how to write in Laravel, Yii2, or any other framework. Don't know the basics of PHP and OOP? PSR for you Prosto Sdelay Raboty? It doesn’t matter, we will teach you, for only 19,990 rubles. A person pays and thinks that he will be taught to write right immediately. Did you write right right away?







I began to notice that for some reason, people stopped understanding the database (working with databases, functions, OOP, startup, reflection, working with files, a network, and so on), and they switched directly to frameworks. They don’t know how it works from the inside, they just write on the framework, and not with the help. They are not worried about what File :: save () does, the strength of encapsulation is in the same. And it’s not interesting. And if something is not in this framework, then this function cannot be done.







But I do not agree with this approach to learning. Cycling is the best way to understand any technology (well, it will begin now). Talking over a cup of coffee somehow, I said a silly thought: if you want to understand PHP, write your CMS with plugins, permissions, ext. fields and admin panel. And then I thought. Or maybe the idea is not stupid at all?







How does learning through frameworks work?



So, use composer create-project ...., then go to .env, edit access for the database, then we put the controller classes here, the model classes here, the template classes here. We seemed to have learned, but we did not understand how it works from the inside. A little exaggerated, but the way it is.







I met several people who said that they write on frameworks. But at the same time they do not know the basics. One person told me in all seriousness that he did not know the array_map function.







Okay, what will our bike give us?



At least we will get acquainted with the autoload of files and classes. We learn that this can be done using composer, and not write your spl_autoload_ ... We will learn how to create database connections, using reflection we will learn how to build our plug-in system and understand why \ $ _ GET and \ $ _ POST in business logic is bad. We will go all the way from Request to Response. The frameworks go this way for us.







Yes, almost all frameworks are written perfectly. In the learning process, you must watch how certain things are done with them. If you write your “mega framework”, you will have to do reverse engineering and understand how the same Yii2 or Laravel works. And maybe you will come to a stupor when you find out that Laravel is just a bunch of components. A side effect of this is a deep understanding of the framework and its internal architecture. And in this mode, it seems to me, understanding comes much faster. At least personally with me.







Having stepped on all the rakes, we will understand at a deep level why these or those design approaches and patterns are used, especially when you need to expand, maintain and test your code.







On forums and social networks, they only discuss that bicycles are bad. When I see an article on some website where a person talks about his home project, whether it be a social network, a blog or a site with a catalog of cats that he wrote from scratch or on individual components, I am a little happy. A person learns from the base and it’s good!







After all, how else to study? Did you immediately start writing working and accurate code without errors? I no, I wrote thousands of lines of code before I realized what was happening. Although maybe I didn’t understand, but does it all seem to me?







But it’s better to write your bike at home, understand the technology, understand what's what, maybe even find a ready-made solution on packagist, than write your bike on production.







The article from mail.ru is close in spirit to me: Modern PHP without frameworks , in which the author wanted to show the device of all the frameworks and what they really are.







Recently, a lot of Legacy code from the 2000s has become in my life. The project is old, but interesting. Which also needs to be transferred to new rails (if it is interesting, I will definitely tell). It looks about the same as the code at the beginning of the article: layout, sql and logic in one file. But I have a legacy shot. I know how to work with him and how to refactor him. But what will a person who can write only on frameworks do?







He just will not face such a project :)








All Articles