close
以下資料來源
PHP 5 新功能簡介
1. 支援名稱空間(namespace)
namespace This {
class Hoge {
}
const aConstant = "This Constant";
function aFunction() {}
var $aVariable = "This Variable";
}
// 使用名稱空間內的成員(變數或函數)
// [語法] 名稱空間 :: 成員
$obj =new This :: Hoge;
echo This :: aConstant."";
This :: aFunction();
echo This :: $aVariable."";
2. 使用 const 定義常數
define('constant_value', 'global constant');
class MyClass {
const constant_value ='class constant';
function printConstant() {
print constant_value;
}
}
echo MyClass :: constant_value."";
MyClass :: printConstant();
? >
3. 定義靜態成員(類別成員)
引用靜態成員的語法 className::staticMember
php
class Hoge {
static $my_static = 5;
}
print Hoge :: $my_static;
? >
4. 建構子
[語法] 以 _ _construct 來定義建構子
class BaseClass {
function _ _ construct() {
print "In BaseClass constructor";
}
}
class SubClass extends BaseClass {
function _ _ construct() {
parent :: _ _construct();
print "In SubClass constructor";
}
}
$obj =new BaseClass();
$obj =new SubClass();
? >
5. 解構子
[語法] 以 _ _ destruct 來定義解構子
php
class MyDestructableClass {
function _ _ construct() {
print "In constructor";
$this->name = 'MyDestructableClass';
}
function _ _ destruct() {
print 'Destroying'.$this->name."";
}
}
$obj =new MyDestructableClass();
?>
6. 動態增加 class 成員(類似javascript語法)
class Hoge {
}
$obj = new Hoge;
$obj->prop = "This is new property";
7. 參數傳遞型式可以傳值或傳參考
php
function some _ func( $var ==null) {
if ($var ==null) {
// 特別的處理
}
}
? >
8. 例外處理
使用 try-catch 區塊處理例外
class MyException {
function _ _ construct {
$this- >exception =$exception;
}
function Display() {
print "MyException: $this- >exception";
}
}
class MyExceptionFoo extends MyException {
function _ _ construct {
$this- >exception =$exception;
}
function Display() {
print "MyExceptionFoo: $this->exception";
}
}
try {
throw new MyExceptionFoo('Hello');
}
catch (MyException $exception) {
$exception- >Display();
}
?>
9. 存取權限
class 的成員可以為 public、protected 或 privateclass A {
private $x;
public function funcA() {
print("I'm public");
}
10. 介面 Interfaces
使用 implement 關鍵字實作介面內宣告的functioninterface A {
function funcA();
}
interface B {
function funcB();
}
class C implements A,B {
function funcA() {
// code...
}
function funcB() {
// code...
}
}
11. 新增 _ _call、_ _set 和 _ _get 的使用
12. 其他新的SimpleXML延伸部份,可以容易地存取及處理XML。
.全新的內建SOAP延伸部份,可以交互處理網站服務。
.新的MySQL延伸部份,名稱為MySQLi for developers,使用MySQL 4.1或更新版本。除了傳統的介面之外,這個新的延伸部份包含了物件導向的介面。如此可以支援許多MySQL新功能,例如prepared statements。
.SQLite已被包入PHP。想了解更多有關SQLite的資訊,請觀看他們的網站。
.資料流(Streams)已經大幅度的改善,包括存取在stream上的低階socket運作。
全站熱搜
留言列表