PHP5.3中的新变化

What has changed in PHP 5.3.x

PHP5.3有什么新变化

Most improvements in PHP 5.3.x have no impact on existing code

大部分PHP5.3中的改进对现存的代码没有影响

There are a few incompatibilities and new features that should be considered, and code should be tested before switching PHP versions in production environments.

需要考虑一小部分不兼容的代码和新功能。

并且代码在应用到不同版本的生产环境,应当被测试

Although most existing PHP 5 code should work without changes, please take note of some backward incompatible changes:

尽管大多数现存的PHP5代码可以不需要改动就可以运行,但还是要注意一些向后不兼容的改变

 The newer internal parameter parsing API has been applied across all the extensions bundled with PHP 5.3.x. This parameter parsing API causes functions to return NULL when passed incompatible parameters. There are some exceptions to this rule, such as the get_class() function, which will continue to return FALSE on error.

新的内部参数解析API以应用到所有捆绑在PHP5.3上的扩展.

当传递不兼容的参数时到函数时,这个内部参数解析API将返回NULL

这条规则将产生一些异常,比如get_class函数,将继续在出错时返回FALSE

clearstatcache() no longer clears the realpath cache by default.

clearstatcache()函数将默认不清除realpath缓存

realpath() is now fully platform-independent

realpath()现在已经完全平台独立

The call_user_func() family of functions now propagate $this even if the callee is a parent class.

The array functions natsort(), natcasesort(), usort(), uasort(), uksort(), array_flip(), and array_unique() no longer accept objects passed as arguments. To apply these functions to an object, cast the object to an array first.

array函数natsort(), natcasesort(), usort(), uasort(), uksort(), array_flip(), 和 array_unique()不再接受object类型的传入.想要在object上使用这些函数,先将object转换为array。

The behaviour of functions with by-reference parameters called by value has changed. Where previously the function would accept the by-value argument, a warning is now emitted and all by-ref parameters are set to NULL.

函数通过值传入引用类型的参数的行为发生了变化.之前函数可以接受,现在将导致一个警告.引用类型的参数将被设置为NULL.

The new mysqlnd library necessitates the use of MySQL 4.1's newer 41-byte password format. Continued use of the old 16-byte passwords will cause mysql_connect() and similar functions to emit the error, "mysqlnd cannot connect to MySQL 4.1+ using old authentication."

新的mysqlnd库需要使用MySQL 4.1新的41-byte的密码格式

使用旧的16字节的密码将导致mysql_connect()和类似的函数产生一个错误:mysqlnd cannot connect to MySQL 4.1+ using old authentication. (mysqlnd不能用旧的验证方式来连接到MySQL 4.1以上的版本)

The trailing / has been removed from the SplFileInfo class and other related directory classes.

SplFileInfo和相关的目录类移除了尾随的/

The __toString magic method can no longer accept arguments.

__toString魔术方法不再接受参数

The magic methods __get, __set, __isset, __unset, and __call must always be public and can no longer be static. Method signatures are now enforced.

魔术方法__get, __set, __isset, __unset, and __call 必须是public的并且不再允许是静态(static)的.

现在是强制的.

The __call magic method is now invoked on access to private and protected methods.

__call魔术方法现在可以调用私有和保护 (private and protected) 的方法了

 

 

The following keywords are now reserved and may not be used in function, class, etc. names.

下面的关键字被保留了并且不能被用在函数、类等 的名字中了。

goto
namespace

 

goto的使用方法示例1

<?php
goto a
;
echo 
'Foo'
;
 
a
:
echo 
'Bar'
;
?>

示例2

<?php
for($i=0,$j=50$i<100$i
++) {
  while(
$j
--) {
    if(
$j==17) goto end

  }  
}
echo 
"i = $i"
;
end
:
echo 
'j hit 17'
;
?>

goto不能跳转到loop或switch中

 

 

 

<?php
namespace my\name

class MyClass {}
function 
myfunction() {}
const 
MYCONST 1;

$a = new MyClass;
$c = new \my\name\MyClass;
$a strlen('hi');

$d = namespace\MYCONST;
$d __NAMESPACE__ '\MYCONST';
echo 
constant($d); 
?>

 

In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions:

 

  1. Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
  2. Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.

在PHP的世界里,命名空间设计为解决程序和库的作者在创建可重用的类或函数时导致的两个问题

一、在你创建的类、函数、常数和PHP内部或第三方的名称起冲突

二、使用别名或更短的名称的能力的同时减轻第一个问题,改善源码的可读性。

示例

 

<?php
namespace my\name// see "Defining Namespaces" section

class MyClass {}
function 
myfunction() {}
const 
MYCONST 1;

$a = new MyClass;
$c = new \my\name\MyClass// see "Global Space" section

$a strlen('hi'); // see "Using namespaces: fallback to global
                   // function/constant" section

$d = namespace\MYCONST// see "namespace operator and __NAMESPACE__
                        // constant" section
$d __NAMESPACE__ '\MYCONST';
echo 
constant($d); // see "Namespaces and dynamic language features" section
?>
 
PS:PHP5.3中还新增了用户自定义的php.ini、闭包、sqlite3的支持等功能

« 上一篇 | 下一篇 »

Comment (require):