What you should know when writing PHP now
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 on. You need to know most of the terms and concepts. 1.PSRThe 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 currently has 6 documents approved: 0: Automatic loading (mainly for versions without namespace before PHP 5.3) 1: Coding specifications 2: Coding style recommendations 3: Log result 4: Autoloading is more detailed (a big change after the emergence of namespaces) 7: HTTP message interface Currently in drafting 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. ComposerComposer 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 method for the implementation and extension management of 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 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-fixerThe 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 purpose 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. Project address: https://github.com/FriendsOfPHP/PHP-CS-Fixer The 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. 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.PsySHA runtime developer console, interactive debugger and REPL for PHP. PsySH is a PHP interactive running environment 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 frameWhat I prefer is Laravel. Currently, the company is using Yii2. I am paying attention to 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- to 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 an additional vendor directory and the introduction of autoload.php files). ORMORM 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 considering 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 engineA template engine needs to do three things: Output of variable value (echo), Conditional judgment and looping (if ... else, for, foreach, while) Introduced or inherited 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-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. Private blog address: 0x1.im |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
