Table of Contents
Traits
其他语法改进
命令行 Web 服务器
弃用特性
Home php教程 php手册 PHP 5.4语法改进与弃用特性

PHP 5.4语法改进与弃用特性

Jun 13, 2016 am 09:39 AM
php

PHP 5.4于本月尘埃落定,它是 PHP 自 2009 年以来的首次重大更新。该版本对语言部分进行了增强,包括支持 Traits 和移除部分争议特性。

Traits

同 Java 和 .NET 一样,PHP 使用单一继承模型。虽然这种模型足够应付大部分用例场景,但有时用户也需要将一些通用样板代码放置其他无关联的类中。(.NET 的 Dispose 方法就是这类样板代码(boiler-plate code)中一个很好的例子。)为解决这样的问题,PHP5.4推出了 Traits。

Traits 是一个方案集合,它不属于任何实际类。用户没法创建 Trait 实例或直接调用 Trait 中的方法,相反,用户必须将 Traits 合并到实际的类当中方能使用它们。在优先级方面,Trait 方法会覆盖继承而来的同名方法,而当前合并类中的同名方法则会覆盖 Trait 方法。

Traits 之间拥有相同的优先级,因此默认情况下,将两个包含重叠方法名的 Traits 加入到相同类中是个错误的做法。当然,使用“insteadof”和“as”操作符可以手工解决冲突,从而避免这个错误。此外,“as”操作符还可以用于改变 Trait 的可见性(visibility)。

Traits 中的方法可以是抽象方法,用于支持 Traits 的部分泛化;同时,Traits 也可以对外暴露静态变量,每个包含 Traits 的类都将拥有该变量的一份拷贝。此外,在 Traits 中已经声明过的属性不可以再在包含它的类中声明。

其他语法改进

其他语法改进包括:

  • 数组现在可以使用方括号进行声明,而不再需要使用“array”函数。新的声明方式除了略微简洁之外,没有其他影响;
  • 现在可以直接对函数返回值进行数组取值。在此之前,函数返回值的结果必须先存入一个临时变量中,如:$FirstName = GetNames ();
  • PHP 5.4 中的闭包现在可以使用“$this”指针,这在以前是不可以的;
  • 现在可以在实例化时访问类成员,如: (new CustomerService)->Delete (customerId);
  • 采用“0b”前缀标识二进制数。

命令行 Web 服务器

PHP 5.4 提供了自己的 Web 服务器,你可以从命令行中启动它。该 Web 服务器的设计目的仅在于帮助开发,它不会取代你的生产 Web 服务器。

弃用特性

备受指责的 Register Globals 已从 PHP 中完全删除。十年来,该特性一直以其频繁发生的安全漏洞而著称。2002年该特性被设置为默认关闭。2009年发布的 PHP5.3 将该特性标记为“弃用”,想必从那时起,大部分开发人员已经不再使用它。

从 PHP 中移除的另一个不讨喜的特性是 Magic Quotes。Magic Quotes 本意是对字符串进行自动转义(escape)以试图避免 SQL 注入攻击。但是由于字符串的转义使用方法常与特定背景相关,因此,比起试图解决的问题,它反而造成了更多的问题。该特性同 Register Globals 一样,也在 2009 年被标记为“弃用”。

PHP 中的 break 和 continue 语句之后可以跟上一个参数用来指明跳出的循环层数。如果不指定参数,它会像 VB、C#或 Java 一样跳出最内层的循环。在 PHP 5.4 之前,开发人员可以向 break 语句传递一个变量,而现在只能传递常量。

PHP 允许参数按引用传递。在早期版本中,你可以通过为调用点添加修饰来指明变量按引用传递。在 PHP 5.4 中,该选项已被移除。相反,现代 PHP 编程只需要在函数声明时指定按引用传递即可。与 C# 不同,你不需要同时在声明和调用点指定按引用传递。

查看英文原文:http://www.infoq.com/news/2012/03/PHP-5.4

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles