php中mysql和mysqli_php mysqli中-和::有什么区别?
->用來(lái)訪問(wèn)實(shí)例的成員,一般左面是一個(gè)類實(shí)例(或者$this), 右面是一個(gè)函數(shù)或者屬性. 箭頭也可以通過(guò)類實(shí)例來(lái)訪問(wèn)靜態(tài)函數(shù).
::用來(lái)訪問(wèn)靜態(tài)成員、常量,訪問(wèn)父類中的成員. 一般左面是一個(gè)類名,或self、parent、static關(guān)鍵字, 右面是一個(gè)靜態(tài)函數(shù)、靜態(tài)屬性、常量.<?php
// 費(fèi)了我好大功夫, 把這些都總結(jié)到一個(gè)例子里面了
// PHP5.3 PHP5.4 PHP5.5 測(cè)試通過(guò)
class A
{
//類常量
const constValue = "constValue\n";
//類屬性
public $property = "properties\n";
//靜態(tài)屬性
static public $staticProperty = "staticProperty\n";
//普通函數(shù)
public function func()
{
}
//靜態(tài)函數(shù)
static public function staticFunc()
{
}
}
$xxoo = new A;
//訪問(wèn)實(shí)例的屬性
print $xxoo->property;
//訪問(wèn)實(shí)例的函數(shù)
print $xxoo->func();
//也可以借助實(shí)例來(lái)訪問(wèn)靜態(tài)成員
//訪問(wèn)函數(shù)要用箭頭,屬性要用雙冒號(hào)
print $xxoo->staticFunc();
print $xxoo::$staticProperty;
//如果不借助實(shí)例,那就直接用類名加雙冒號(hào)
print A::staticFunc();
print A::constValue;
//靜態(tài)屬性要加美元符號(hào)
print A::$staticProperty;
//繼承類A
class B extends A
{
//覆蓋父類的屬性
public $property = "covered-properties\n";
//覆蓋父類的靜態(tài)屬性
static public $staitcProperty = "covered-staitcProperties\n";
//覆蓋父類的函數(shù)
public function func()
{
//訪問(wèn)自己的屬性
print $this->property;
//訪問(wèn)從父類繼承來(lái)的靜態(tài)屬性
print self::$staticProperty;
//訪問(wèn)自己的(靜態(tài))函數(shù)
print $this->staticFunc();
//強(qiáng)制指定訪問(wèn)父類(而不是自己)的函數(shù)
print parent::func();
}
//覆蓋父類的靜態(tài)函數(shù)
static public function staticFunc()
{
//因?yàn)闆](méi)有$this, 所以用self訪問(wèn)自己的靜態(tài)屬性
print self::$staitcProperty;
}
}
//運(yùn)行一下上面的例子
$xxoo = new B;
$xxoo->func();
B::staticFunc();
總結(jié):
箭頭:通過(guò)實(shí)例訪問(wèn)屬性、(靜態(tài))函數(shù)
雙冒號(hào):通過(guò)類名或self、parent、static關(guān)鍵字,訪問(wèn)常量、靜態(tài)屬性、靜態(tài)函數(shù)
http://php.net/manual/zh/language.oop...
PHP5.3新增后期靜態(tài)綁定功能,和雙冒號(hào)以及static關(guān)鍵字有關(guān),這個(gè)我正在學(xué)習(xí)中.....
總結(jié)
以上是生活随笔為你收集整理的php中mysql和mysqli_php mysqli中-和::有什么区别?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: t3 修改UFO服务器地址,t3ufo报
- 下一篇: ActiveMQ入门实例