Things you need to pay attention to when learning PHP now

WBOY
Release: 2016-07-25 08:45:51
Original
836 people have browsed it

You should have read php The Right Way. This article contains a lot of content and can be expanded. You need to know most of the terms and concepts. The idea behind the group is for project representatives to talk about the commonalities between our projects and find ways we can work together. In the previous article And during the communication with colleagues, I mentioned PSR (PHP Standard Recommendation) many times. Many people think that PSR only does insignificant things such as standardizing coding style, but it is actually much more than that.

  PSR’s series of standard documents are drafted and voted on by php-fig (PHP Framework Interop Group). Among the voting members are authors of some mainstream frameworks and extensions, including Laravel, Symfony, Yii, etc.

According to its official website, the purpose of this organization is not to tell you what you should do, but to negotiate and agree with each other among some mainstream frameworks. But I believe that you will always use these frameworks and extensions.

There are currently 6 documents approved by PSR:

0: Automatic loading (mainly for versions before PHP 5.3 without namespace) 1: Coding specifications

2: Coding style Recommended

 3: Log results

 4: Automatic loading is more detailed (there are big changes after the namespace appears)

  7: HTTP message interface

 Currently in drafting (Draft) Also included are PSR-5 (PHPDoc Standard), PSR-6 (Cache), etc. 5 and 6 are not on the list above because they have not yet been voted on.

I believe that as the standards continue to be updated, you will find that studying these conventions is also very beneficial to you, although you may not necessarily abide by everything.

 Nobody in the group wants to tell you, as a programmer, how to build your application.

  2.Composer

 Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

  Composer is different from Pear and Pecl. It is not only used to install extensions, but more importantly, it defines a Implementation and extension management of modern PHP frameworks. Similar to npm for node.js and pip for

Python

, but does more than the above.

 The core of composer is to implement standard installation of extensions and automatic loading of classes. Through the packagist.org platform, countless extension components can be easily introduced. Currently, the more well-known PHP extensions can be installed through composer. The call only needs to load an autoload.php file.

 Composer registers an automatic loading method through the spl_autoload_register method to load extension classes and files. Of course, composer also made an optimization during this process.

We all know that PHP import files must be implemented through include and require, which is actually not good-looking to write. PHP 5.3 provides namespaces, which have nothing to do with file import. However, composer implements PSR-4 (PSR-0 on older versions of PHP). When using use, the method implemented by calling spl_autoload_register loads the required classes when called. The writing method is similar to Python's import, which is also beautiful. It comes to the role of on-demand loading and lazy loading.

  3.php-cs-fixer

   The PHP Coding Standards Fixer tool fixes most issues in your code when you want to follow the PHP coding standards as defined in the PSR-1 and PSR-2 documents.

 The function of this tool is to format your code according to PSR-1 and PSR-2 specifications, and some optional coding styles are Symfony specifications.

In fact, this is not worth talking about originally, but I recently saw .php_cs files in several open source frameworks. I was curious and found out about this project after further research.

  Project address: https://github.com/FriendsOfPHP/PHP-CS-Fixer

  Specific usage and configuration methods are introduced on its project homepage. The name of this organization is also interesting: FriendsOfPHP. The main members are probably from the Symfony project.

Some people may think that there is no need to worry about coding style issues. I can't tell you the benefits. If you think programming is more than just a job, then it's like cleaning your room. A dirty room won't affect your eating and sleeping, but a clean one will look more comfortable. If you want to cooperate with others, then this matter is even more important.

  4.PsySH

  A runtime developer console, interactive debugger and REPL for PHP.

  PsySH is an interactive runtime environment for PHP similar to Python's IDLE. I discovered this in Laravel, and the functionality of Laravel 5’s artisan tinker is implemented through it. Laravel 4 uses another project: boris.

 This is mainly useful when testing some simple functions and features of PHP. If you encounter something uncertain, such as the use of empty, you can use it to do some testing.

5. Some frameworks and components

Framework

What I prefer is Laravel. The company is currently using Yii2. I am concerned about Symfony and Phalcon (C language implementation). What to use and what not to use mainly comes down to preference, and sometimes you can’t help but make your own choice, but it’s not a bad idea to do some research and learn more about it.

When mentioning Laravel, many people will immediately think of Ruby on Rails. I think imitating or plagiarizing is not the main purpose. The main purpose is to provide developers with a better tool. Fortunately, Laravel has a different routing control (without Action suffix or prefix), a useful ORM (Eloquent), a useful template engine (Blade), or a relatively good-looking document ( If the community sees it) etc.

  Powerful is sometimes criticized for being huge, but this is because you need to understand the mid- and long-term planning of your project, the current size of the project, and the future size and load.

 The core implementation of Larval is a container (Container) and PHP’s reflection class (ReflectionClass) (the same is true for Yii 2). To understand this, while reading more articles and documents, you can also look at the source code.

 Symfony 2 provides many components. http-kernel and http-foundation are also inherited and used directly in Laravel. It is worth knowing and learning.

 CodeIgniter is a small but powerful framework. Although CI is not developed using Composer components, versions after 3.0 also add Composer support (this is nothing more than an additional vendor directory and the introduction of the autoload.php file).

 ORM

 ORM or Active Record I think is still needed. Some people may think that PHP is just a template engine and SQL should be written by hand. Don't be bothered by these words.

 The implementation of Active Record in CodeIgniter is very lightweight, but for the size of CI itself, it is already very useful.

 I like Eloquent implemented by Laravel very much, and it can also be integrated into other projects. Symfony 2 uses Doctrine, and this project is also worthy of attention. Yii 2 also has its own set of implementation methods.

  Template engine

The template engine needs to do three things:

1. Output of variable values ​​​​(echo),

2. Conditional judgment and looping (if ... else, for , foreach, while)

  3. Introduce or inherit from other files

  Blade implemented by Laravel is a relatively lightweight and easy-to-use template engine. However, it is currently not very easy to introduce it into other frameworks. When I was free, I tried to introduce it into Yii 2. Now it is just a simple implementation. I hope to extract the parsing part of Blade separately and make a lightweight implementation later. A search on Github found someone doing the same thing.

  Yii 2 seems to be more recommended to use native PHP to write, but it also provides extensions that support Smarty and Twig. Symfony 2 uses Twig. Twig and Symfony, as well as the php-cd-fixer mentioned above, are all works of SensioLabs.

 Smarty is an old and tenacious template engine. To be honest, I don't like it very much. Its syntax is too complicated, and things like variable assignment have their own set of methods. The current version uses Lexer to parse files, which feels like another language implemented in PHP. There are also some regular expressions that are too long and implementations that are too complex in the project. I think this is a very dangerous and error-prone thing. More related PHP information http://www.kokojia.com/list/219.html




source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template