Now write PHP, you should know these

Source: Internet
Author: User
Tags autoload php file php coding standards php framework version variable yii ruby on rails

First of all you should be in PHP 5.3 above version, if PHP version is under this, it is time to upgrade. I suggest that if you have the conditions, you'd better use the latest version.

You should have seen PHP right Way, this article contains a lot of content, but also can be extended. Most of the terms and concepts you need to understand.

1. PSR

The idea behind the group are for project representatives to talk about the commonalities between we projects and find way s we can work together.

I have referred to PSR (PHP Standard recommendation) Many times in previous articles and in the process of interacting with colleagues. Many people think that PSR is only to do some standard code style, such as insignificant things, but in fact far more than this.

A series of standard documents for PSR are drafted and voted by Php-fig (PHP Framework Interop Group), with a number of mainstream frameworks and extended authors, including Laravel, Symfony, Yii, and so on.

According to its official website, the purpose of the organization is not to tell you what you should do, but to negotiate and agree on some of the mainstream frameworks. But I believe there is always something you can use in these frameworks and extensions.

PSR currently adopts a total of 6 documents:

    • 0: Automatic loading (mainly for PHP 5.3 has no namespace before the version)

    • 1: Coding Specifications

    • 2: Coding style recommended

    • 3:log Results

    • 4: Automatic loading is finer (there is a big change after the namespace appears)

    • 7:http Message Interface

Currently in the drafting (Draft) there are PSR-5 (PHPDoc Standard), PSR-6 (Cache) and so on. 5 and 6 do not appear in the above list because they have not been voted on.

I believe that with the constant updating of the standards, you will find that the study of these conventions is also very beneficial to you, although not necessarily everything to comply.

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

2. Composer

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

Composer and Pear, Pecl are different, it is not only for installation expansion, more importantly, defines a modern PHP framework implementation and extended management methods. Like Node.js's NPM, Python's Pip, but more than that.

The core of composer is the implementation of extended standard installation and automatic loading of classes. By this platform, countless extension components can be easily introduced, the current more well-known PHP extensions can be installed through the composer. The call only needs to load a autoload.php file.

Composer is the Spl_autoload_register method to register an automatic loading method to implement extension classes and file loading, of course, this intermediate composer also made an optimization.

We all know that PHP introduces files to be implemented through include and require, which is not really good to write. PHP 5.3 Provides a namespace, which is irrelevant to the introduction of the file. But composer implemented PSR-4 (in the old version of PHP is PSR-0), using use when the call to the Spl_autoload_register implementation of the method to load the required classes, in writing similar to the Python import, both beautiful and The function of loading on demand and delaying loading.

3. Php-cs-fixer

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

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

This is actually not so worthwhile to say, but recently in several open source framework has been seen. Php_cs's paper, a curious, dig down to find this project.

Project Address: Https://

Specific usage and configuration methods are described on their project home page. The name of the organization is also interesting: friendsofphp. The main members are probably from the Symfony project.

Maybe some people think that the problem of tangled code style is not particularly necessary. If you think that programming is more than just a job, it's just like cleaning your room, sloppy rooms don't affect your eating and sleeping, but the clean looks more comfortable. This is even more important if you want to work with others.

4. Psysh

A Runtime Developer console, interactive debugger and REPL for PHP.

Psysh a PHP interactive environment similar to Python's IDLE. This is what I found in Laravel, the function of Laravel 5 artisan Tinker is achieved through it. Laravel 4 uses another project: Boris.

This is mainly in the normal time to test some of the simple PHP functions and features can be easily used. Encounter some uncertain things, such as the use of empty, you can use it to do some testing.

5. Some frameworks and components


I prefer is laravel, at present the company is using YII2, I am concerned about the Symfony and Phalcon (C language Implementation). What not to use what, mainly is the hobby, sometimes also by not own choice, but studies, more than one point understanding also may not be.

When it comes to laravel, many people will immediately think of Ruby on Rails. I would like to imitate or copy this is not the main purpose, the main purpose is to provide developers with a better tool. Laravel The good news is that it has a different routing control (without an Action suffix or prefix), a handy ORM (eloquent), a good template engine (Blade) or a document with a higher color value (what the community sees), and so on.

Strong can sometimes be criticized, but it is you need to understand the long-term planning of your project, the size of the project now and the size and bearing of the future.

The core implementation of larval is a container (Container) and a Reflection class (Reflectionclass) in PHP (Yii 2 is the same). To understand these, read more articles and documents at the same time, you can also look at the source code.

Symfony 2 provides a number of components. Http-kernel and Http-foundation were also inherited and used directly in Laravel. It is worth understanding and learning.

CodeIgniter is a compact and powerful framework. Although CI was not developed using the Composer component, the version after 3.0 added Composer support (this is nothing more than a vendor directory, the introduction of autoload.php) files.


ORM or Active record I think it's still needed. Perhaps some people think that PHP is a template engine, it should be handwritten SQL. Don't be bothered by these words.

The Active record in CodeIgniter is implemented in a lightweight way, but it is already very useful for the volume of the CI itself.

Laravel implementation of the eloquent I am very fond of, can also be integrated into other projects. Symfony 2 uses the doctrine, and this project is also worth paying attention to. Yii 2 also has its own set of implementations.

Template engine

The template engine needs to do three things:

    1. The output of the variable value (ECHO),

    2. Conditional Judgments and loops (if ... else, for, foreach, while)

    3. Introduce or inherit from other files

Laravel implementation of the Blade is a relatively lightweight and easy to use the template engine. But it's not very good to be introduced into other frameworks at the moment. When 11 is free to try to introduce it into Yii 2, now it's just a simple implementation, and I hope that the Blade's parsing section can be extracted separately for a lightweight implementation. Search on the Github and find that someone else is doing the same thing.

Yii 2 seems to be more recommended to write in native PHP, but it also provides support for Smarty and Twig extensions. The Symfony 2 uses Twig. Twig and Symfony, as well as the php-cs-fixer mentioned above, are all sensiolabs works.

Smarty is an old and tenacious template engine. To be honest, I'm not too fond of it, its syntax is too complex, variable assignment these things have their own set of practices. Now in the version is to use the Lexer way to parse the file, it feels like using PHP to implement a different language. There are too long regular expressions in the project, too complex implementation, I think this is a very dangerous and error-prone things.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.