望php手冊2015-03-19版後備忘

來源:互聯網
上載者:User
看php手冊2015-03-19版後備忘

類與對象->基本概念:
1,#############################
::class
自 PHP 5.5 起,關鍵詞 class 也可用於類名的解析。使用 ClassName::class 你可以擷取一個字串,包含了類 ClassName 的完全限定名稱。這對使用了 命名空間 的類尤其有用。
Example #7 類名的解析as stated in the docs is:
namespace NS {
class ClassName {
}
echo ClassName::class;
}
?>
以上常式會輸出:
NS\ClassName


2,#############################
Just to be clear: the correct way of validating a classname, as stated in the docs is:
$valid = preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $className);

3,#############################
屬性中的變數可以初始化,但是初始化的值必須是常數,這裡的常數是指 PHP 指令碼在編譯階段時就可以得到其值,而不依賴於運行時的資訊才能求值
PHP 5.3.0 新增 支援Nowdoc聲明類屬性; 不包含變數的heredoc也是可以的,包含變數就錯。


new static() 會遵循繼承關係,new 的是子類
new self() 不會被繼承,new 的是 self 這個詞所在的那個類


###
As of PHP 5.6 you can finally define constant using math expressions, like this one:

class MyTimer {
const SEC_PER_DAY = 60 * 60 * 24;
}

?>

###
自 PHP 5.3.3 起,在命名空間中,與類名同名的方法不再作為建構函式。這一改變不影響不在命名空間中的類。
namespace Foo;
class Bar {
public $a;
public function Bar() {
$this->a = 'to here';
}
public function getA(){
return $this->a;
}
}
$bar = new Bar();
echo $bar->getA(); //空 ; 去掉命名空間則輸出 to here;



###
自 PHP 5.3.0 起,可以通過變數來引用類,該變數的值不能是關鍵字(如 self,parent 和 static)。




帶整理:
self,parent 和 static
public(公有),protected(受保護)或 private(私人)

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.