因为用到PHP新版本,一些新特性必须要了解,且有些可以在开发时就使用,如果不使用,那么何必升级PHP版本呢,显得有些得不偿失了! 所以整理了一下 一些特性,有可能不全,待添加 1.PHP 5.3中的新特性 1.1 PHP 5.3中的新特性 1.1.1. 支持命名空间 (Namespace
因为用到PHP新版本,一些新特性必须要了解,且有些可以在开发时就使用,如果不使用,那么何必升级PHP版本呢,显得有些得不偿失了!
<?php
namespace Zend\Db\Table;
class Select {} <?php
//namespace Zend\Db;
include('select.php');
$s = new Zend\Db\Table\Select();
$s->test(); 在PHP5中,我们可以在类中通过self关键字或者__CLASS__来判断或调用当前类。但有一个问题,如果我们是在子类中调用,得到的结果将是父类。因为在继承父类的时候,静态成员就已经被绑定了。 例如:
<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test(); <?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // 这里实现了延迟的静态绑定
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test(); <?php
goto a;
echo 'Foo';
a:
echo 'Bar';
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto end;
}
}
echo "i = $i";
end:
echo 'j hit 17';<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// 输出 helloWorld
$greet = function($name)
{
printf("Hello %s\r\n", $name);
};
$greet('World');
$greet('PHP');
//...在某个类中
$callback = function ($quantity, $product) use ($tax, &$total) {
$pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($products, $callback);<?php
class MethodTest {
public function __call($name, $arguments) {
// 参数 $name 大小写敏感
echo "调用对象方法 '$name' "
. implode(' -- ', $arguments). "\n";
}
/** PHP 5.3.0 以上版本中本类方法有效 */
public static function __callStatic($name, $arguments) {
// 参数 $name 大小写敏感
echo "调用静态方法 '$name' "
. implode(' -- ', $arguments). "\n";
}
}
$obj = new MethodTest;
$obj->runTest('通过对象调用');
MethodTest::runTest('静态调用'); // As of PHP 5.3.0 <?php
class MethodTest {
public function __call($name, $arguments) {
// 参数 $name 大小写敏感
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}
/** PHP 5.3.0 以上版本中本类方法有效 */
public static function __callStatic($name, $arguments) {
// 参数 $name 大小写敏感
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
}
$obj = new MethodTest;
$obj->runTest('in object context');
MethodTest::runTest('in static context'); // As of PHP 5.3.0<?php // Nowdoc 单引号 PHP 5.3之后支持 $name = 'MyName'; echo <<<'EOT' My name is "$name". EOT; //上面代码输出 My name is "$name". ((其中变量不被解析) // Heredoc不加引号 echo <<<FOOBAR Hello World! FOOBAR; //或者 双引号 PHP 5.3之后支持 echo <<<"FOOBAR" Hello World! FOOBAR;
<?php
// 静态变量
function foo()
{
static $bar = <<<LABEL
Nothing in here...
LABEL;
}
// 类成员、常量
class foo
{
const BAR = <<<FOOBAR
Constant example
FOOBAR;
public $baz = <<<FOOBAR
Property example
FOOBAR;
}//PHP中定义常量通常是用这种方式
define("CONSTANT", "Hello world.");
//并且新增了一种常量定义方式
const CONSTANT = 'Hello World';//原格式 $expr=$expr1?$expr1:$expr2 //新格式 $expr=$expr1?:$expr2
class Test{
public static function testgo()
{
echo "gogo!";
}
}
$class = 'Test';
$action = 'testgo';
$class::$action(); //输出 "gogo!" 1.1.6. ereg 正则表达式函数 不再默认可用,请使用速度更快的pcre 正则表达式函数
详细的请看 http://blog.csdn.net/baiwz/article/details/9077825
PHP 5.3.0 新增了两个错误等级: E_DEPRECATED 和 E_USER_DEPRECATED. 错误等级 E_DEPRECATED 被用来说明一个函数或者功能已经被弃用. E_USER_DEPRECATED 等级目的在于表明用户代码中的弃用功能, 类似于 E_USER_ERROR 和 E_USER_WARNING 等级.
下面是被弃用的 INI 指令列表. 使用下面任何指令都将导致 E_DEPRECATED 错误.
define_syslog_variables
register_globals
register_long_arrays
safe_mode
magic_quotes_gpc
magic_quotes_runtime
magic_quotes_sybase
弃用 INI 文件中以 '#' 开头的注释.
弃用函数:
call_user_method() (使用 call_user_func() 替代)
call_user_method_array() (使用 call_user_func_array() 替代)
define_syslog_variables()
dl()
ereg() (使用 preg_match() 替代)
ereg_replace() (使用 preg_replace() 替代)
eregi() (使用 preg_match() 配合 'i' 修正符替代)
eregi_replace() (使用 preg_replace() 配合 'i' 修正符替代)
set_magic_quotes_runtime() 以及它的别名函数 magic_quotes_runtime()
session_register() (使用 $_SESSION 超全部变量替代)
session_unregister() (使用 $_SESSION 超全部变量替代)
session_is_registered() (使用 $_SESSION 超全部变量替代)
set_socket_blocking() (使用 stream_set_blocking() 替代)
split() (使用 preg_split() 替代)
spliti() (使用 preg_split() 配合 'i' 修正符替代)
sql_regcase()
mysql_db_query() (使用 mysql_select_db() 和 mysql_query() 替代)
mysql_escape_string() (使用 mysql_real_escape_string() 替代)
废弃以字符串传递区域设置名称. 使用 LC_* 系列常量替代.
mktime() 的 is_dst 参数. 使用新的时区处理函数替代.
弃用的功能:
弃用通过引用分配 new 的返回值.
调用时传递引用被弃用.
$ php -S localhost:3300
$ php -S localhost:3300 -t /path/to/root
$ php -S localhost:3300 router.php
官网的一个例子:
trait SayWorld {
public function sayHello() {
parent::sayHello();
echo "World!\n";
echo 'ID:' . $this->id . "\n";
}
}
class Base {
public function sayHello() {
echo 'Hello ';
}
}
class MyHelloWorld extends Base {
private $id;
public function __construct() {
$this->id = 123456;
}
use SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
/*will output:
Hello World!
ID:123456
*/$arr = [1,'james', 'james@fwso.cn']; $array = [ "foo" => "bar", "bar" => "foo" ];
function myfunc() {
return array(1,'james', 'james@fwso.cn');
}$arr = myfunc(); echo $arr[1];
echo myfunc()[1];
$name = explode(",", "Laruence,male")[0];
explode(",", "Laruence,male")[3] = "phper";$./configure --with-mysqli=mysqlnd
$./configure --with-mysqli
class test{
function show(){
return 'test';
}
}
echo (new test())->show();foreach ([new Human("Gonzalo"), new Human("Peter")] as $human) {
echo $human->{'hello'}();
}function foo(callable $callback) {
} foo("false"); //错误,因为false不是callable类型
foo("printf"); //正确
foo(function(){}); //正确
class A {
static function show() {
}
}
foo(array("A", "show")); //正确class bar {
function foo(bar $foo) {
}
//其中函数foo中的参数规定了传入的参数必须为bar类的实例,否则系统会判断出错。同样对于数组来说,也可以进行判断,比如:
function foo(array $foo) {
}
}
foo(array(1, 2, 3)); // 正确,因为传入的是数组
foo(123); // 不正确,传入的不是数组这个是用来统计服务请求时间的,并用ms来表示
echo "脚本执行时间 ", round(microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"], 2), "s";
echo json_encode("中文", JSON_UNESCAPED_UNICODE);
//"中文"$bin = 0b1101; echo $bin; //13
可能还有 100 个小的改动和特性。从 PHP 5.3 升级到 5.4 应该极为顺畅,但请阅读迁移指南加以确保。如果您从早期版本升级,执行的操作可能稍多一些。请查看以前的迁移指南再开始升级。
备受指责的 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# 不同,你不需要同时在声明和调用点指定按引用传递。
//从数据库获取一列,但返回是数组。
$userNames = [];
foreach ($users as $user) {
$userNames[] = $user['name'];
}
//以前获取数组某列值,现在如下
$userNames = array_column($users, 'name');
$password = "foo";
// creating the hash
$hash = password_hash($password, PASSWORD_BCRYPT);
// verifying a password
if (password_verify($password, $hash)) {
// password correct!
} else {
// password wrong!
} function randomHexString($length) {
$str = '';
for ($i = 0; $i < $length; ++$i) {
$str .= "0123456789abcdef"[mt_rand(0, 15)]; // direct dereference of string
}
}
function randomBool() {
return [false, true][mt_rand(0, 1)]; // direct dereference of array
} use Some\Deeply\Nested\Namespace\FooBar;
// does not work, because this will try to use the global `FooBar` class
$reflection = new ReflectionClass('FooBar');
echo FooBar::class;function create_query($where, $order_by, $join_type='', $execute = false, $report_errors = true) { ... }create_query("deleted=0", "name", default, default, false);function foo(int $i) { ... }
foo(1); // $i = 1
foo(1.0); // $i = 1
foo("1"); // $i = 1
foo("1abc"); // not yet clear, maybe $i = 1 with notice
foo(1.5); // not yet clear, maybe $i = 1 with notice
foo([]); // error
foo("abc"); // error<?php
class TimePeriod {
public $seconds;
public $hours {
get { return $this->seconds / 3600; }
set { $this->seconds = $value * 3600; }
}
}
$timePeriod = new TimePeriod;
$timePeriod->hours = 10;
var_dump($timePeriod->seconds); // int(36000)
var_dump($timePeriod->hours); // int(10)<?php
function *xrange($start, $end, $step = 1) {
for ($i = $start; $i < $end; $i += $step) {
yield $i;
}
}
foreach (xrange(10, 20) as $i) {
// ...
}$firstNames = [foreach ($users as $user) yield $user->firstName];
$firstNames = [];
foreach ($users as $user) {
$firstNames[] = $user->firstName;
}$underageUsers = [foreach ($users as $user) if ($user->age < 18) yield $user];
$array = [
[1, 2],
[3, 4],
];
foreach ($array as list($a, $b)) {
echo "A: $a; B: $b\n";
}echo array(1, 2, 3)[0]; echo [1, 2, 3][0]; echo "foobar"[2];
原文连接http://cn2.php.net/manual/zh/migration56.new-features.php
允许常量计算,允许使用包含数字、字符串字面值和常量的标量表达式
立即学习“PHP免费学习笔记(深入)”;
const A = 2;
const B = A + 1;
class C
{
const STR = "hello";
const STR2 = self::STR + ", world";
}function test($arg = C::STR2)
代替 func_get_args()
function add(...$args)
{
$result = 0;
foreach($args as $arg)
$result += $arg;
return $result;
}function add($a, $b, $c) {
    return $a + $b + $c;
}
$arr = [2, 3];
add(1, ...$arr);namespace Name\Space {
const FOO = 42;
function f() { echo __FUNCTION__."\n"; }
}
namespace {
use const Name\Space\FOO;
use function Name\Space\f;
echo FOO."\n";
f();
}42 Name\Space\f
<?php $a = gmp_init(42); $b = gmp_init(17); // Pre-5.6 code: var_dump(gmp_add($a, $b)); var_dump(gmp_add($a, 17)); var_dump(gmp_add(42, $b)); // New code: var_dump($a + $b); var_dump($a + 17); var_dump(42 + $b);
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号