Comprehensive Summary of PHP 5–8 Version Features
This article provides a detailed overview of the major features introduced in PHP versions 5 through 8, including autoloading, PDO, MySQLi, namespaces, traits, built‑in server, scalar type declarations, nullable types, JIT compilation, named arguments, union types, match expressions, and many useful code examples.
Overview
The article lists the new language features and improvements introduced in each PHP release from 5.1 up to 8.0, accompanied by concise code snippets that illustrate their usage.
PHP 5.x Features
PHP 5.1
autoload – automatic class loading via function __autoload($className) { $filePath = "project/class/{$className}.php"; if (is_readable($filePath)) { require $filePath; } }
PDO – lightweight database abstraction layer. Example installation and usage:
extension=pdo.so // Linux
extension=php_pdo.dll // Windows <?php
$dbms = 'mysql';
$host = 'localhost';
$dbName = 'test';
$user = 'root';
$pass = '';
$dsn = "$dbms:host=$host;dbname=$dbName";
try {
$dbh = new PDO($dsn, $user, $pass);
echo "连接成功<br/>";
$dbh = null;
} catch (PDOException $e) {
die("Error!: " . $e->getMessage() . "<br/>");
}
?>MySQLi – improved MySQL extension. Example:
$conn = mysqli_connect('localhost', 'root', '123', 'db_test') or die('error');
$sql = "select * from db_table";
$query = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($query)) {
echo $row['title'];
}Type constraints – limit parameter types to classes, callable, or array:
function MyFunction(MyClass $a, callable $b, array $c) { /* ... */ }PHP 5.2
Added native JSON support with json_encode and json_decode .
PHP 5.3
Introduced namespaces, anonymous functions (closures), new magic methods __callStatic() and __invoke() , magic constant __DIR__ , dynamic static method calls, late static binding, heredoc/nowdoc, class‑level constants, ternary operator simplification, and Phar archives.
// Namespace example
namespace XXX;
// Anonymous function
$fn = function($x) { return $x * 2; };
// Magic method usage
$person = new Person('小明');
$person(); // triggers __invoke()PHP 5.4
Features: short open tag <?= , array shorthand [$key => $value] , Traits for code reuse, built‑in web server ( php -S localhost:8000 ), dynamic static method access, and instance member access.
// Trait example
trait SayWorld { public function sayHello() { echo 'World!'; } }
class MyHelloWorld { use SayWorld; }
$obj = new MyHelloWorld();
$obj->sayHello();PHP 5.5
Added yield for generators, list() in foreach , and several deprecations (e.g., old mysql_* functions).
function number10() {
for ($i = 1; $i <= 10; $i++) {
yield $i;
}
}PHP 5.6
Constant enhancements, variadic functions, and namespace improvements.
const A = 2;
const B = A + 1;
function add(...$args) { /* ... */ }PHP 7.x Features
PHP 7.0
Scalar type declarations, return type declarations, define with arrays, anonymous classes, and null‑coalescing operator ?? .
function typeString(string $a) { echo $a; }
function returnErrorArray(): array { return []; }
$username = $_GET['user'] ?? 'nobody';PHP 7.1
Nullable types ( ?string ), void return type, and multiple exception catching.
function testReturn(): ?string { return 'elePHPant'; }
function swap(&$left, &$right): void { /* ... */ }
try { /* ... */ } catch (FirstException | SecondException $e) { /* ... */ }PHP 7.2
New object type hint and ability to override abstract methods with covariant returns.
PHP 7.3
No major syntax changes.
PHP 7.4
Typed properties, arrow functions, null‑coalescing assignment ??= , and Opcache preloading.
class User { public int $id; public string $name; }
$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1,2,3,4]);
$array['key'] ??= computeDefault();PHP 8.0 Features
JIT Compilation
JIT is integrated into Opcache, generating native machine code for supported x86 CPUs.
Named Arguments
balance(amount: 100, payment: 20);Attributes (Annotations)
Allows attaching metadata to classes, methods, and properties.
Union Types
function printSomeThing(string|int $value) { var_dump($value); }Match Expression
$str = match($key) {
'a' => 'this a',
'c' => 'this c',
0 => 'this 0',
'b' => 'last b',
};Nullsafe Operator
$user = null;
echo $user?->getName(); // returns null without errorConstructor Property Promotion
class User {
public function __construct(protected string $name, protected int $age) {}
}The article concludes with references to original tutorials and documentation for further reading.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.