What you need to be aware of in PHP now

Source: Internet
Author: User
Tags autoload coding standards php coding standards php framework codeigniter yii ruby on rails

You should have seen PHP the right, and this article contains a lot of content and can be extended again. Most of the nouns and concepts you need to know.

1.PSR

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

I have mentioned PSR (PHP standard recommendation) many times in previous articles and in the process of communicating with colleagues. Many people think that PSR is just doing something innocuous like code style, but it's much more than that.

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

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

There are 6 documents currently in use in PSR:

0: Auto-load (mainly for versions with no namespace prior to PHP 5.3)

1: Coding Specification

2: Coding Style Recommendation

3:log Results

4: Auto-load finer (there is a big change after the namespace has occurred)

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 list above because they have not yet been voted on.

I believe that as standards are constantly being updated, you will find it beneficial to study these conventions, although not all of them must be adhered to.

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 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 the installation of extensions, more importantly, the definition of a modern PHP framework implementation and extension of management methods. Like node. JS's npm, Python Pip but more than that.

The core of composer is the implementation of the extended standard installation and automatic loading of classes. By packagist.org this platform, countless extension components can be easily introduced, now more well-known PHP extensions can be installed through composer. Instead, the call simply 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 the introduction of PHP files to be implemented through include and require, which is actually not very good to write. PHP 5.3 Provides a namespace, which is irrelevant to the introduction of the file. But composer implements the PSR-4 (which is PSR-0 on older versions of PHP), using the Invoke Spl_autoload_register implementation method to load the required class when invoked, in the same way as the Python import, which is aesthetically pleasing To the role of on-demand loading, lazy loading.

3.php-cs-fixer

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

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

This is actually not so worthwhile to say, but recently in a few open source frameworks have seen. Php_cs's paper, a moment of curiosity, went down to find the project.

Project Address: Https://github.com/FriendsOfPHP/PHP-CS-Fixer

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

There may be people who think that the problem of tangled code style is not particularly important. I can't say if you think that programming is more than just a job, it's like cleaning up your room, and a sloppy room doesn't affect your eating and sleeping, but it's clean and looks more comfortable. If you want to work with others, it's more important.

4.PsySH

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

Psysh a PHP-like interactive runtime environment similar to that of Python. This is what I found in the Laravel, Laravel 5 of the artisan Tinker function is through it to achieve. Laravel 4 is another project: Boris.

This is mostly useful when testing simple functions and features of PHP in the usual time. There are some uncertainties, such as the use of empty and so on, you can use it to do some testing.

5. Some frameworks and components

Framework

I prefer the Laravel, the company is using the YII2, I am concerned about Symfony and Phalcon (C language Implementation). What not to use what, mainly like, and sometimes not by their own choice, but research, more than a point to understand also can not.

When it comes to Laravel, many people immediately think of Ruby on Rails. I want to imitate or copy this is not the main purpose, the main purpose is to provide developers with a better tool. Laravel Fortunately it has a different routing control (without the Action suffix or prefix), there is a good ORM (eloquent), a good template engine (Blade) also has a high value of the document (community see) and so on.

Power can sometimes be criticized as large, but this is because you need to know your project's medium and long-term planning, the size of the project and the future size and load.

The core implementation of larval is a container (Container) and the Reflection Class (Reflectionclass) of PHP (also the same for Yii 2). To understand this, 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 in the Laravel also have been inherited to come directly to use. It is worth knowing and learning.

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

Orm

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 easy to implement, but it is very useful for the volume of CI itself.

Laravel implementation of the eloquent I am very fond of, but also can be integrated into other projects. Symfony 2 uses Doctrine, and the project deserves attention. Yii 2 also has its own set of implementation methods.

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

The Blade implemented by Laravel is a relatively lightweight and easy-to-use template engine. But it's not good enough to be introduced into other frameworks at the moment. When I was 11, I tried to introduce it into the Yii 2, and now it's just a simple implementation, and I hope that the parsing part of the Blade can be extracted separately to make a light-weight implementation. Search on Github and find someone doing the same thing.

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

Smarty is an old and tenacious template engine. To tell the truth, I'm not too fond of it, its syntax is too complex, variable assignment these things have their own set of practices. The current version of the use of Lexer to parse the file, it feels like a PHP implementation of another language. The project also has some too long regular expressions, too complex implementations, I think this is a very dangerous and error-prone things. More about PHP Data http://www.kokojia.com/list/219.html


  • 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: info-contact@alibabacloud.com 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.