Home  >  Article  >  PHP is no longer what it was ten years ago

PHP is no longer what it was ten years ago

藏色散人
藏色散人forward
2019-05-15 16:38:156742browse

Developer Brent published an article detailing what PHP is like in 2019.

PHP is no longer what it was ten years ago

Brent admitted that PHP still has some problems, such as many core functions still have inconsistent method signatures and configuration settings are still confusing, but in his own From the perspective of development experience, he believes that PHP is an excellent choice for Web development. Using PHP, he can create reliable, maintainable and high-quality applications, and both himself and his customers are satisfied with the final results. (Recommended study: PHP7 Technology Topic)

In the article, Brent focuses on the good aspects of PHP's development over the years. He explains through a few points that "PHP is no longer It was a terrible language ten years ago”:

● PHP is actively being developed and new versions will be released every year

● After PHP 5, the performance is constantly improving

● There is a very active language ecosystem composed of frameworks, packages and platforms

● PHP is constantly developing, and many new features have been added in the past few years

● Various development tools have been developed in the past It has matured in several years and continues to grow

PHP is actively developed and new versions will be released every year

The current PHP version is 7.3 released in December last year, 7.4 is expected Released at the end of this year, PHP 8.0 will be the next version after 7.4.

PHP is no longer what it was ten years ago

Since the 5.X era, the core team has hoped to release a new version every year, and they have successfully maintained such a release cycle for the past four years. Typically each new version is officially actively supported for two years, followed by a year of only security fixes, with the goal of incentivizing PHP developers to stay updated as much as possible.

PHP is no longer what it was ten years ago

For more specific release and maintenance cycles, you can view the introduction on the PHP official website:

https://www.php.net/supported- versions.php

Looking at the table above, you may find that the version number jumped from 5 to 7. So where did PHP 6 go?

By the way, let’s do some popular science. In fact, as early as 2005, the PHP community initiated PHP 6. However, due to the difficulties in implementing Unicode, the project eventually aborted. PHP officials did not release PHP 6 GA, and canceled PHP 6 in 2010. Although the PHP 6 project was cancelled, a large number of its implemented features were integrated into PHP 5.

PHP 6 was cancelled, but previous development of this major version was conducted under the name PHP 6, so many reference materials and books use "PHP 6" to refer to this version. Officials were worried that this would cause confusion after abandoning PHP 6, so they simply did not use the PHP 6.X version number and jumped directly from 5 to 7.

For details, please check the official instructions:

https://wiki.php.net/rfc/php6

Performance after PHP 5 In continuous improvement

In PHP 7.0, the core part of PHP has been completely rewritten, bringing the latest Zend engine, which improves PHP performance by two to three times.

The author quoted the benchmark test at https://kinsta.com/blog/php-benchmarks to illustrate this point, pointing out that since 7.0, PHP performance has been improving, and PHP web applications and web frameworks in other languages The performance is much better than in some cases. But he also said: "Of course the PHP framework will not be better than C and Rust, but it is much better than Rails or Django, and comparable to ExpressJS."

There are very active frameworks, packages and platforms The language ecology

When it comes to the PHP framework, we have to mention WordPress, but the author said that WordPress definitely does not represent the current PHP ecology: "PHP framework is no longer just WordPress."

There are currently two main web application development frameworks in PHP: Symfony and Laravel, as well as Zend, Yii, Cake and Code Igniter, in addition to many smaller frameworks.

Both Symfony and Laravel have huge package and software ecosystems, including admin panel and CRM, independent software packages, CI, analyzers, Web socket servers, queue managers and payment integrations, etc.

In addition, PHP asynchronous frameworks have also emerged in recent years. Swoole, Amp and ReactPHP are the best among them. They are frameworks and servers written in PHP or other languages ​​to run true asynchronous PHP.

The author believes that one way to measure the current state of the PHP ecosystem is to look at the data from Packagist, the main PHP package repository. Through the following trend chart, you can see that the number of software packages has increased exponentially, and there are currently 223,217 registered software packages, and the total number of installations has reached 14,827,204,847. The PHP ecosystem is no longer as weak as before.

PHP is no longer what it was ten years ago

Number of available packages of various versions

PHP is no longer what it was ten years ago

Number of packages installed per month

PHP is no longer what it was ten years ago

In addition, you can also check out the latest data on PHP usage statistics and market positioning:

http://www.php.cn/toutiao-418943.html

PHP is constantly evolving, and many new features have been added in the past few years

Although async and await, two highly requested features, have not yet been implemented, However, new features of PHP have continued to appear in recent years, and the language itself has been improved in various aspects.

Listed below are some of the new PHP features that have attracted a lot of attention:

● Short closures

Short closures, also known as arrow functions , which is a way to write shorter functions in PHP. Closures can be extremely useful when passed to functions like array_map or array_filter.

// A collection of Post objects $posts = [/* … */];
$ids = array_map(fn($post) => $post->id, $posts);

● Null coalescing operator

Null coalescing operator, it is similar to the ternary operator, but the left operand behaves like isset, instead of just using its boolean value. This makes this operator particularly useful for arrays. It also assigns a default value when the variable is not set.

$undefined ?? 'fallback'; // 'fallback'
$unassigned;
$unassigned ?? 'fallback'; // 'fallback'
$assigned = 'foo';
$assigned ?? 'fallback'; // 'foo'
'' ?? 'fallback'; // ''
'foo' ?? 'fallback'; // 'foo'
'0' ?? 'fallback'; // '0'
0 ?? 'fallback'; // 0
false ?? 'fallback'; // false

● Traits

Trait is a mechanism for reusing code that can reduce some of the limitations of single inheritance. The semantics of combining Traits with classes define a way to reduce complexity and avoid typical problems associated with multiple inheritance and Mixins.

Trait is similar to a class, but is only used to group functions in a fine-grained and consistent manner. It is a supplement to traditional inheritance and can achieve horizontal combination of behaviors. Class member applications do not require inheritance.

<?php
trait ezcReflectionReturnInfo {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}
class ezcReflectionMethod extends ReflectionMethod {
    use ezcReflectionReturnInfo;
    /* ... */
}
class ezcReflectionFunction extends ReflectionFunction {
    use ezcReflectionReturnInfo;
    /* ... */
}
?>

● Typed properties

Typed properties, class variables can define types, such as:

class A
{
    public string $name;
    
    public Foo $foo;
}

● Spread operator

Expand operator, this is a new syntax that can decompress parameters directly in the call, such as:

call_user_func_array([$db, &#39;query&#39;], array_merge(array($query), $params));

The array $params can be expanded directly using the new syntax:

$db->query($query, ...$params);

● JIT compiler: JIT support is confirmed in PHP 8

JIT is a compiler strategy that expresses code as an intermediate state and converts it into an intermediate state at runtime. It is converted to architecture-dependent machine code and executed on the fly. In PHP, this means that the JIT treats instructions generated by Zend VM as intermediate representations and executes them in architecture-dependent machine code. That is to say, it is no longer Zend VM that hosts the code, but the underlying CPU. .

● FFI

Foreign Function Interface, which allows calling C functions from pure scripting languages ​​and using C data types, thereby more efficiently developing "systems" code". For PHP, FFI opens up a way to write PHP extensions and bind to C libraries using pure PHP.

● Anonymous classes

Anonymous classes for creating simple one-time objects:

<?php
// Pre PHP 7 code
class Logger
{
    public function log($msg)
    {
        echo $msg;
    }
}
$util->setLogger(new Logger());
// PHP 7+ code
$util->setLogger(new class {
    public function log($msg)
    {
        echo $msg;
    }
});

● Return type declarations

Declare return type:

<?php
function sum($a, $b): float {
    return $a + $b;
}
// Note that a float will be returned.
var_dump(sum(1, 2));
?>

Strict mode:

<?php
declare(strict_types=1);
function sum($a, $b): int {
    return $a + $b;
}
var_dump(sum(1, 2));
var_dump(sum(1, 2.5));
?>

● Contemporary cryptography

Make Libsodium a core extension, Libsodium is A modern cryptographic library consisting of encryption algorithms carefully selected by security experts to avoid side-channel vulnerabilities.

● Generators

Generators provide a simple, boilerplate-free way to implement iterators.

Various development tools have matured in the past few years and have continued to grow

In the last part, the author analyzed that the current PHP development tools have also matured and continued to grow.

He used static analyzers as examples, such as Psalm, Phan and PHPStan. These tools will statically analyze PHP code and report any type of errors and possible bugs. To some extent, the functionality they provide is comparable to that of TypeScript, but since PHP currently does not provide transpile, custom syntax is not allowed. This means that PHP development needs to rely on documentation, but in fact PHP's creator Rasmus Lerdorf once mentioned the idea of ​​adding a static analysis engine to the core.

受到 JavaScript 社区的启发,目前 PHP 也有在进行转换的相关研发,比如项目 Pre,它允许新的 PHP 语法转换为普通的 PHP 代码。虽然这个想法已经在 JavaScript 中被证明可行,但作者认为在 PHP 中,只有先提供了适当的 IDE 和静态分析支持,它才有可能实现。

讲完了 PHP 当前的这些变化,作者最后没有以“PHP 是世界上最好的语言”作结语,相反,他是这样说的:

All that being said, feel free to still think of PHP as a crappy language... I can say in confidence that I enjoy working with it.

话虽如此,但是还要继续认为 PHP 是蹩脚的语言那也是可以的。我可以很自信地说我喜欢 PHP。

Statement:
This article is reproduced at:oschina.net. If there is any infringement, please contact admin@php.cn delete