Home >Backend Development >PHP Tutorial >Do you really understand current PHP?
Some time ago, the company's project was upgraded from PHP5.3 to PHP7. Now the project starts to use some of PHP7 New syntax and features. Looking back at PHP versions 5.4, 5.5, and 5.6, there is a sense of lack of knowledge. Therefore, I decided to read "Modern PHP" to supplement some concepts.
Reading this book
1. Features
<?php use Symfony\Component\HttpFoundation\Request, Symfony\Component\HttpFoundation\Response, Symfony\Component\HttpFoundation\Cookie;
It is recommended to write one per line. use statement:
<?php use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Cookie;Use multiple namespaces in one file
"One file defines a class"
Good practice.<?php namespace Foo { //code } namespace Bar { //code }Global namespace
<?php namespace My\App; class Foo { public function doSomething() { $exception = new \Exception(); } }#. ##If there is no \ symbol before Exception, the Exception class will be found in the My\App namespace 2. Use
Why use traits? For example, there are two classes, Car and Phone, both of which require GPS functionality. In order to solve this problem, the first reaction is to create a parent class and let Car and Phone
inherit it.It. But because it is obvious that this ancestor does not belong to the respective inheritance hierarchy.
The second reaction is to create a GPS interface, define the GPS functional interface, and then make the two classes Car and Phone. Both implement this interface. This can achieve functionality while maintaining the natural inheritance hierarchy. However, this requires duplicate GPS functions, which does not comply with the DRY (dont repeat yourself) principle ##. #The third reaction is to create a trait that implements the GPS function, and then mix this trait into the Car and Phone classes. It can realize functions, does not affect the inheritance structure, does not repeat implementation, and is perfect.
Creating and using traits
<?php trait MyTrait{ //实现 }Using traits
<?php class MyClass { use MyTrait; // 类的实现 }4.
Generator
PHP generator (generator) is a new feature introduced in PHP5.5.0, which many PHP developers do not understand. The generator is a simple iterator, but the generator is not required to implement the
. The generator computes and produces the values to be iterated over as needed. Without
querying<?php
function makeRange($length) {
for ($i = 0; $i < $length; $i++) {
yield $i;
}
}
foreach (makeRange(1000000) as $i) {
echo $i, PHP_EOL;
}
Specific scenario: Use the generator to process CSV files
<?php function getRows($file) { $handle = fopen($file, 'rb'); if ($handle === false) { throw new Exception(); } while (feof($handle) === false) { yield fgetcsv($handle); } } foreach (getRows('data.csv') as $row) { print_r($row); }
To deal with this scenario, the customary method is to first read all the contents of the file and put them into the
array, and then do processing and so on. The problem with this kind of processing is: When the file is very large, it takes up a lot of memory resources to read it at one time. The generator is most suitable for this scenario because it takes up very little system memory.
5. ClosureTheoretically, closure and anonymous function are different concepts. However, PHP treats it as the same concept. Simple closure
<?php $closure = function ($name) { return sprintf('Hello %s', $name); } echo $closure("Beck"); // 输出 --> “Hello Beck”Note: The reason why we can call $closure
variable is because the value of this variable is a closure, and closureObject
implements invoke()magic method. As long as there is () after
, PHP will find and call the invoke() method. 附加状态 使用bindTo()方法附加闭包的状态 第11行是重点所在,把路由回调绑定到了当前的App实例上。这么做能在回调函数中处理App实例的状态: 字节码缓存不是PHP的新特性,很多独立的扩展可以实现缓存。从PHP5.5.0开始,PHP内置了字节码缓存功能,名为Zend OPcache。 字节码缓存是什么 从PHP5.4.0起,PHP内置了Web服务器,这对众多使用Apache或nginx的php开发者来说,可能是个隐藏功能。不过,这个内置的服务器功能并不完善,不应该在生产环境中使用,但对本地开发来说是个便利的工具,可以用于快速预览一些框架和应用。 启动服务器 路由器脚本 是否为内置的服务器
使用use关键字可以把多个参数传入闭包,此时要像PHP函数或方法的参数一样,使用逗号分隔多个参数。<?php
function enclosePerson($name) {
return function ($doCommand) use ($name) {
return sprintf('%s, %s', $name, $doCommand);
};
}
// 把字符串“Clay”封装在闭包中
$clay = enclosePerson('Clay');
// 传入参数,调用闭包
echo $clay('get me sweet tea!');
// 输出 --> "Clay, get me sweet tea!"
PHP框架经常使用bindTo()方法把路由URL映射到匿名回调函数上,框架会把匿名函数绑定到应用对象上,这么做可以在这个匿名函数中使用$this关键字引用重要的应用对象。例子如下:<?php
class App
{
protected $routes = array();
protected $responseStatus = '200 OK';
protected $responseContentType = 'text/html';
protected $responseBody = 'Hello world';
public function addRoute($routePath, $routeCallback)
{
$this->routes[$routePath] = $routeCallback->bindTo($this, CLASS);//重点
}
public function dispatch($currentPath)
{
foreach ($this->routes as $routePath => $callback) {
if ($routePath === $currentPath) {
$callback();
}
}
header('HTTP/1.1' . $this->responseStatus);
header('Content-type:' . $this->responseContentType);
header('Content-length' . mb_strlen($this->responseBody));
echo $this->responseBody;
}
}
<?php
$app = new App();
$app->addRoute('/users/josh', function () {
$this->responseContentType = 'application/json;charset=utf8';
$this->responseBody = '{"name": "Josh"}';
});
$app->dispatch('/users/josh');
6. Zend OPcache
PHP是解释性语言,PHP解释器执行PHP脚本时会解析PHP脚本代码,把PHP代码编译成一系列Zend操作码,然后执行字节码。每次请求PHP文件都是这样,会消耗很多资源。字节码缓存能存储预先编译好的PHP字节码。这意味着,请求PHP脚本时,PHP解释器不用每次都读取、解析和编译PHP代码。这样能极大地提升应用的性能。7. 内置的HTTP服务器
php -S localhost:4000
php -S localhost:8000 -c app/config/php.ini
与Apache和nginx不同,它不支持.htaccess文件。因此,这个服务器很难使用多数流行的PHP框架中常见的前端控制器。PHP内置的服务器使用路由器脚本弥补了这个遗漏的功能。处理每个HTTP请求前,会先经过这个路由器脚本,如果结果为false,返回当前HTTP请求中引用的静态资源URI。php -S localhost:8000 route.php
<?php
if (php_sapi_name() === 'cli-server') {
// php 内置的web服务器
}
The above is the detailed content of Do you really understand current PHP?. For more information, please follow other related articles on the PHP Chinese website!