Home  >  Article  >  Backend Development  >  What you should know when writing PHP now

What you should know when writing PHP now

迷茫
迷茫Original
2017-01-17 09:48:271114browse

First of all, you should be using PHP 5.3 or above. If the PHP version is below this, it is time to upgrade. I recommend using the latest version if possible.

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.

1. PSR

The idea behind the group is for project representatives to talk about the commonalities between our projects and find ways we can work together.

I have mentioned PSR (PHP Standard Recommendation) many times in previous articles and during discussions with colleagues. 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.

PSR has currently approved 6 documents:

0: Automatic loading (mainly for versions without namespaces before PHP 5.3)

1: Coding specifications

2: Coding style recommendations

3: Log results

4: Automatic loading is more detailed (there is a big change after the namespace appears)

7 :HTTP message interface

Currently in draft, there 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 for Installing extensions, and more importantly, defines a method for implementing and managing extensions in a modern PHP framework. Similar to npm of node.js and pip of 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 imports files 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 calling. The writing method is similar to Python's import, which is both beautiful and functional. 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.

This is actually not worth talking about originally. I just recently saw .php_cs files in several open source frameworks. I was curious and found out about this project after further research. Some people may think that there is no need to worry about coding style. 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 for PHP similar to Python's IDLE environment. I discovered this in Laravel, and the function of artisan tinker in Laravel 5 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 some uncertain things, 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. Currently, the company is using Yii2. I am concerned about Symfony and Phalcon (implemented in C language). 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 carrying capacity.

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 later than 3.0 also add support for Composer (this is nothing more than adding an additional vendor directory and introducing autoload.php files).

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. Introduced or inherited from other files

The 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-cs-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.

Statement:
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