ในวันเดียวกันกับที่ ปล่อย Firefox 3.5 ทาง PHP เองก็ออก PHP 5.3 มาเช่นกันมาดูกันว่ามีอะไรเปลี่ยนไป
1.NOWDOC & HEREDOC
แต่ก่อนคุณเคยใช้ HEREDOC
$test = <<< HTML
.....
HTML;
ก็ต้องเปลี่ยนมาเป็น
$test = <<< "HTML"
.....
HTML;
และเพิ่ม NOWDOC - เหมือน HEREDOC แต่เป็น static text ไม่ประมวลผลตัวแปร
$test = <<< 'HTML' //ใช้ single quote
$foo is {$foo}
HTML;
//output $foo is {$foo}
เพิ่มเติม :
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
2. Jump to line
คือสามารถ jump จากบรรทัดหนึ่งไปอีกบรรทัดหนึ่งได้
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto end;
}
}
echo "i = $i";
end:
echo 'j hit 17';
เพิ่มเติม :
http://www.php.net/manual/en/control-structures.goto.php
3.native Closures
คือการที่สามารถกำหนดให้ตัวแปนเป็น function ได้
$greet = function($name)
{
printf("Hello %s\r\n", $name);
};
$greet('World');
$greet('PHP');
เพิ่มเติม :
http://www.php.net/manual/en/functions.anonymous.php
4. Const Vs define
ใน php version นี้การใช้ const จะเหมือนกับกันใช้ define
//Old version
define('CONSTANT','Hello World');
//This Works as of PHP 5.3.0
const CONSTANT = 'Hello World';
echo CONSTANT;
เพิ่มเติม :
http://www.php.net/manual/en/language.constants.syntax.php
5.Namespace
สามารถใช้ namespace ได้
file1.php
namespace Foo\Bar\subnamespace;
const FOO = 1;
function foo() {}
class foo
{
static function staticmethod() {}
}
file2.php
namespace Foo\Bar;
include 'file1.php';
const FOO = 2;
function foo() {}
class foo
{
static function staticmethod() {}
}
/* Unqualified name */
foo(); // resolves to function Foo\Bar\foo
foo::staticmethod(); // resolves to class Foo\Bar\foo, method staticmethod
echo FOO; // resolves to constant Foo\Bar\FOO
/* Qualified name */
subnamespace\foo(); // resolves to function Foo\Bar\subnamespace\foo
subnamespace\foo::staticmethod(); // resolves to class Foo\Bar\subnamespace\foo,
// method staticmethod
echo subnamespace\FOO; // resolves to constant Foo\Bar\subnamespace\FOO
/* Fully qualified name */
\Foo\Bar\foo(); // resolves to function Foo\Bar\foo
\Foo\Bar\foo::staticmethod(); // resolves to class Foo\Bar\foo, method staticmethod
echo \Foo\Bar\FOO; // resolves to constant Foo\Bar\FOO
เพิ่มเติม :
http://www.php.net/manual/en/language.namespaces.php
และที่เหลือก็จะเป็นการเพิ่ม function class method ... สามารถดูรายละเอียดอีกทีได้ที่
http://www.php.net/manual/en/migration53.php
จะเห็นว่า php มีขีดความสามารถที่เพิ่มขึ้น (หุหุ) และมีบางอย่างที่หายไปเช่น register globals (ซึ่งปกติไม่ควรใช้อยู่แล้ว) จนรุ่นพี่ที่รู้จักกันถึงกับเอ๋ยว่า คนเขียนหนังสือ php งานเข้า
edit @ 1 Jul 2009 02:32:14 by Nodtem32