The PHP 5.4 is now available.
As you probably know, the updates that were intended for postponed PHP 6 were added to PHP 5.4.0 instead, so now PHP includes a huge set of new language features and removes several legacy behaviors.
Because of that I created a list of major changes since PHP 5.3 ,along with some examples and brief descriptions of these changes…
Major PHP improvements
Changes since PHP 5.3 version include:
-
Added class member access on instantiation.
Now you can use fluent interfaces like in Java:
$myCar = (new Car)->setSpeed(100)->setColor('blue');
-
Added callable typehint.
This typehint allows a string with a function name, a closure, and an array composed of classname (or object) with method name.
<?php
function foo(callable $cb) {
$cb();
}
?>
-
Added closure rebinding as parameter to bindTo.
Closure::bindTo() has been modified so now it accepts another argument that defines the new scope. This can either be an object if its class is used as the scope, or a class name.
class A {
private $x;
public function __construct($v) {
$this->x = $v;
}
public function getIncrementor() {
return function() { return ++$this->x; };
}
}
class B extends A {
private $x;
public function __construct($v) {
parent::__construct($v);
$this->x = $v*2;
}
}
$a = new A(0);
$b = new B(10);
$ca = $a->getIncrementor();
var_dump($ca());
echo "Testing with scope given as object", "\n";
$cb = $ca->bindTo($b, $b);
$cb2 = Closure::bind($ca, $b, $b);
var_dump($cb());
var_dump($cb2());
echo "Testing with scope as string", "\n";
$cb = $ca->bindTo($b, 'B');
$cb2 = Closure::bind($ca, $b, 'B');
var_dump($cb());
var_dump($cb2());
$cb = $ca->bindTo($b, NULL);
var_dump($cb());
Result:
int(1)
Testing with scope given as object
int(21)
int(22)
Testing with scope as string
int(23)
int(24)
-
Added short array syntax.
Makes PHP code more readable and maintainable.
$a = [1, 2, 3];
$b = ['foo' => 'orange', 'bar' => 'apple', 'baz' => 'lemon'];
-
Added binary value format.
Now it’s possible to use binary values directly in the PHP code:
-
Added support for Class::{expr}() syntax.
Makes PHP more flexible, when calling class/object methods.
$method = 'method';
$test = new Test();
$test->method();
$test->$method();
$test->{'method'}();
Test::method();
Test::$method();
Test::{'method'}();
Result:
method
method
method
method
method
method
-
Added support for Traits.
A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.
class Base {
public function sayHello() {
echo 'Hello ';
}
}
trait SayWorld {
public function sayHello() {
parent::sayHello();
echo 'World!';
}
}
class MyHelloWorld extends Base {
use SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
Result:
-
Added closure $this support back.
Now you have an access to every object property (be it public or not).
class A {
private $value = 1;
function firstGetter($name) {
return function() use ($name) {
return $this->$name;
};
}
function secondGetter() {
return function($name) {
return $this->$name;
};
}
}
$a = new A();
$firstGetter = $a->firstGetter('value');
echo $firstGetter();
$secondGetter = $a->secondGetter();
echo $secondGetter('value');
Result:
-
Added array dereferencing support.
Provides the implementation of array dereferencing of method/function return.
function fruit () {
return array('a' => 'apple', 'b' => 'banana');
}
echo fruit()['a'];
Result:
-
Added indirect method call through array.
Now $foo() also works in the cases where $foo is a callable array or Closure object.
class Hello {
static public function world($x) {
echo "Hello, $x\n";
}
}
function helloWorld($x) {
echo "Hello, $x\n";
}
$callbacks = array(
array('Hello', 'world'),
function ($x) { echo "Hello, $x\n"; },
'helloWorld'
);
foreach ($callbacks as $k => $callback) {
if (is_callable($callback)) {
$callback($k);
}
}
Result:
Hello, 0
Hello, 1
Hello, 2
-
Changed $GLOBALS into a JIT autoglobal.
$GLOBALS array is initialized only if it’s used. This is a performance/memory optimization, it can however break some of the existing scripts or opcode caches.
-
Improved performance of @ (silence) operator.
This can speed up PHP scripts which rely heavily on a silence operator, for example:
$x = @file_get_contents('/etc/passwd');
echo $x;
-
Added multibyte support by default.
Previously php had to be compiled with –enable-zend-multibyte. Now it can be enabled or disabled through zend.multibyte directive in php.ini.
-
Added built-in web server that is intended for testing purpose.
The following command will open a web server on the port 8000.
- Lots of performance and memory usage improvements
Removed major PHP features
-
Removed break/continue $var syntax.
You can no longer use variable to tell PHP how many levels of enclosing loops it should skip to the end of.
-
Removed safe mode and all related ini options.
Functionality described in this article and marked as depreciated in PHP 5.3 has now been removed
-
Removed register_globals and register_long_arrays ini options.
If enabled, register_globals injected PHP scripts with all sorts of variables, like request variables from HTML forms or values from GET requests. Now, every request/environment variable must be fetched from an appropriate PHP array.
-
Removed allow_call_time_pass_reference option.
Passing arguments by reference at function call time was deprecated for code-cleanliness reasons. A function can modify its arguments in an undocumented way if it didn’t declare that the argument shall be passed by reference. To prevent side-effects it’s better to specify which arguments are passed by reference in the function declaration only.
For a full list of changes in PHP 5.4, see the ChangeLog. For source downloads please visit php.net QAT downloads page, Windows binaries can be found here.
分享到:
相关推荐
基于springboot大学生就业信息管理系统源码数据库文档.zip
基于java的驾校收支管理可视化平台的开题报告
时间序列 原木 间隔5秒钟 20241120
毕业设计&课设_基于 Vue 的电影在线预订与管理系统:后台 Java(SSM)代码,为毕业设计项目.zip
基于springboot课件通中小学教学课件共享平台源码数据库文档.zip
基于java的网上购物商城的开题报告
Delphi人脸检测与识别Demo1fdef-main.zip
基于java的咖啡在线销售系统的开题报告
基于java的自助医疗服务系统的开题报告.docx
内容概要:本文档全面介绍了Visual Basic(VB)编程语言的基础知识和高级应用。首先概述了VB的基本特性和开发环境,随后详细讲述了VB的数据类型、变量、运算符、控制结构、数组、过程与函数、变量作用域等内容。接着介绍了窗体设计、控件使用、菜单与工具栏的设计,文件操作、数据库访问等关键知识点。最后讨论了VB的学习方法、发展历史及其在桌面应用、Web应用、数据库应用、游戏开发和自动化脚本编写等领域的广泛应用前景。 适合人群:初学者和中级程序员,尤其是希望快速掌握Windows桌面应用开发的人群。 使用场景及目标:①掌握VB的基础语法和开发环境;②学会使用VB创建复杂的用户界面和功能完整的应用程序;③理解数据库操作、文件管理和网络编程等高级主题。 其他说明:Visual Basic是一种简单易学且功能强大的编程语言,尤其适合用于开发Windows桌面应用。文中不仅覆盖了基础知识,还包括了大量的实用案例和技术细节,帮助读者快速提升编程技能。
基于java的疫情期间高校防控系统开题报告.docx
基于springboot+vue社区老年人帮扶系统源码数据库文档.zip
基于java的超市商品管理系统的开题报告.docx
基于SpringBoot房屋买卖平台源码数据库文档.zip
xdu限通院23微处理器系统与应用大作业(两只老虎),适应于汇编语言keil软件,
<项目介绍> - 新闻类网站系统,基于SSM(Spring、Spring MVC、MyBatis)+MySQL开发,高分成品毕业设计,附带往届论文 - 不懂运行,下载完可以私聊问,可远程教学 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
基于java的学生网上请假系统的开题报告.docx
社会经济繁荣发展的今天,电子商务得到了飞速发展,网上交易越来越彰显出其独特的优越性,在人们的日常生活中,出现了各种类型的交易网站。其中一个就是车辆易主交易网站,它是一个服务于用户买卖二手车辆的交易网站,为用户提供了平等互利、方便快捷的网上交易平台,通过这一类型的网站,用户可自由出售和购买车辆。 本课题主要根据车辆本身的特性,充分发挥互联网的特点与优势,构建一个以二手车辆为商品、基于互联网平台的车辆易主业务交易管理系统,并根据车辆易主业务交易管理系统的应用需求,进行需求分析,进而对网站系统作规划设计。采用IDEA为运行平台,以SSH为框架,运用HTML语言、JSP技术、MySql数据库、JSP与后台数据库链接等关键技术建设二手车网上交易系统,构建车辆易主交易系统的会员注册与登录,网站首页展示、用户发布商品车辆,用户求购商品车辆,分页浏览、购物系统、用户后台管理、管理员用户后台管理等功能,并使这些功能得以实现并更好为用户服务。网站整体构建完成且测试成功后,用户可以进入网站进行注册、登录,登录后,用户可以在网站上发布自己的闲置车辆或者寻找想要购买的车辆,还可以收藏车辆,管理发布和收藏的车辆,
SQLite3的向量扩展库,windows dll,版本0.1.5
基于C++实现(控制台)商品库存管理系统