>PHP 5.3是一个有很大改进的PHP版本,但它仍然遵循着PHP的设计原则—强大、易用。 PHP5.3一方面在面向对象开发等方面有所加强,使PHP更合适于进行企业应用开发,另一方面也增加了不少实用的语法特性和新扩展。我们期待着它能够早日稳定,成为WEB开发中的又一个利器。
<?php
namespace Zend/Db/Table;
class Select {}
?>
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // 这里实现了延迟的静态绑定
}
}
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto end;
}
}
echo "i = $i";
end:
echo 'j hit 17';
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
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
// 静态变量
function foo()
{
static $bar = <<<LABEL
Nothing in here...
LABEL;
}
// 类成员、常量
class foo
{
const BAR = <<<FOOBAR
Constant example
FOOBAR;
public $baz = <<<FOOBAR
Property example
FOOBAR;
}
define("CONSTANT", "Hello world.");
const CONSTANT = 'Hello World';
expr1 ?: expr3
//如果expr1结果为True,则返回expr1的结果
class Test
{
public static function testgo()
{
echo "gogo!";
}
}
$class = 'Test';
$action = 'testgo';
$class::$action(); //输出 "gogo!"
>此次更新的关键新特性,包括:新增traits,更精简的Array数组语法,供测试使用的内建webserver,可以闭包使用的$this指针,实例化类成员访问,<?=标记永远生效 等等……
### traits
trait Hello
{
public function hello()
{
return 'Hello';
}
}
trait Cichui
{
public function cichui()
{
return ' cichui';
}
}
class HelloCichui
{
use Hello, Cichui;
public function the_end()
{
return '!';
}
}
$o = new HelloCichui;
echo $o->hello(), $o->cichui(), $o->the_end();
echo (new Hello)->hello(), (new Cichui)->cichui(), (new HelloCichui)->the_end();
>个人感觉和javascript中call方法类似
<?php
// router.php
if (preg_match('#\.php$#', $_SERVER['REQUEST_URI']))
{
require basename($_SERVER['REQUEST_URI']); // serve php file
}
else if (strpos($_SERVER['REQUEST_URI'], '.') !== false)
{
return false; // serve file as-is
}
?>
<?php
// index.php
echo 'Hello cichui.com Readers!';
?>
include_path = ".;C:\php\PEAR;/home/mtao/project/test"
php -S 0.0.0.0:8080 -t /home/mtao/project/test webrouter.php
访问:http://localhost:8080/index.php$fruits = array('apples', 'oranges', 'bananas'); // "old" way
// 学Javascript的数组了
$fruits = ['apples', 'oranges', 'bananas'];
// 关联数组
$array = [
'foo' => 'bar',
'bar' => 'foo'
];
echo explode(‘ ‘, ‘Fang Bin Xin’)[1]; // Bin
class Foo
{
function hello() {
echo 'Hello Cichui!';
}
function anonymous()
{
return function() {
$this->hello(); // 之前是不可能这么玩的
};
}
}
class Bar
{
function __construct(Foo $o) // object of class Foo typehint
{
$x = $o->anonymous(); // get Foo::hello()
$x(); // execute Foo::hello()
}
}
new Bar(new Foo); // Hello Cichui!
现在我们不用设置 short_open_tag 直接这样来用了
<?=$title?>
echo 0b11111; // PHP 5.4支持二进制了
echo 31; // 十进制
echo 0x1f; // 十六进制
echo 037; // 八进制
PHP5.5 将不再支持Windows XP和2003,这些系统已经有十年的历史,所以PHP将不再提供支持。
>得到一个变量的布尔值
echo '0: '.(boolval(0) ? 'true' : 'false')."\n";
echo '42: '.(boolval(42) ? 'true' : 'false')."\n";
echo '0.0: '.(boolval(0.0) ? 'true' : 'false')."\n";
>PBKDF2全称“Password-Based Key Derivation Function 2”,正如它的名字一样,是一种从密码派生出加密密钥的算法
<?php
$userNames = array_column($users, 'name');
// is the same as
$userNames = [];
foreach ($users as $user) {
$userNames[] = $user['name'];
}
#### password_hash password_verify
>密码处理
<?php
$password = "foo";
// creating the hash
$hash = password_hash($password, PASSWORD_BCRYPT);
// verifying a password
if (password_verify($password, $hash)) {
// password correct!
} else {
// password wrong!
}
“常量引用”意味着数组可以直接操作字符串和数组字面值。举两个例子:
<?php
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
}
>php5.5之前empty()语言构造只能用在变量,而不能在其他表达式。在特定的代码像empty($this->getFriends())将会抛出一个错误。作为PHP5.5 这将成为有效的代码
php5.5之前多个参数时候,必须全部传递, 现在加上 $report_errors = true 不必全部传递
function create_query($where, $order_by, $join_type='', $execute = false, $report_errors = true) { ... }
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
>如果你从不喜欢写这些getXYZ()和setXYZ($value)方法,那么这应该是你最受欢迎的改变。提议添加一个新的语法来定义一个属性的设置/读取:
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)
$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];