PHP鐨勫熀鏈蹇
姣忎釜绫荤殑瀹氫箟閮戒互鍏抽敭瀛 class 寮澶达紝鍚庨潰璺熺潃绫诲悕锛屽彲浠ユ槸浠讳綍闈 PHP 淇濈暀瀛楃殑鍚嶅瓧銆傚悗闈㈣窡鐫涓瀵硅姳鎷彿锛岄噷闈㈠寘鍚湁绫绘垚鍛樺拰鏂规硶鐨勫畾涔夈備吉鍙橀噺 $this 鍙互鍦ㄥ綋涓涓柟娉曞湪瀵硅薄鍐呴儴璋冪敤鏃朵娇鐢ㄣ$this 鏄竴涓埌璋冪敤瀵硅薄锛堥氬父鏄柟娉曟墍灞炰簬鐨勫璞★紝浣嗕篃鍙互鏄彟涓涓璞★紝濡傛灉璇ユ柟娉曟槸浠庣浜屼釜瀵硅薄鍐呴潤鎬佽皟鐢ㄧ殑璇濓級鐨勫紩鐢ㄣ
class
姣忎釜绫荤殑瀹氫箟閮戒互鍏抽敭瀛 class 寮澶达紝鍚庨潰璺熺潃绫诲悕锛屽彲浠ユ槸浠讳綍闈 PHP 淇濈暀瀛楃殑鍚嶅瓧銆傚悗闈㈣窡鐫涓瀵硅姳鎷彿锛岄噷闈㈠寘鍚湁绫绘垚鍛樺拰鏂规硶鐨勫畾涔夈備吉鍙橀噺 $this 鍙互鍦ㄥ綋涓涓柟娉曞湪瀵硅薄鍐呴儴璋冪敤鏃朵娇鐢ㄣ$this 鏄竴涓埌璋冪敤瀵硅薄锛堥氬父鏄柟娉曟墍灞炰簬鐨勫璞★紝浣嗕篃鍙互鏄彟涓涓璞★紝濡傛灉璇ユ柟娉曟槸浠庣浜屼釜瀵硅薄鍐呴潤鎬佽皟鐢ㄧ殑璇濓級鐨勫紩鐢ㄣ傜湅涓嬮潰渚嬪瓙锛
Example #1 闈㈠悜瀵硅薄璇█涓殑 $this 鍙橀噺
<?php
class A
{
function foo()
{
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ") ";
} else {
echo "$this is not defined. ";
}
}
}
class B
{
function bar()
{
A::foo();
}
}
$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();
?>
浠ヤ笂渚嬬▼浼氳緭鍑猴細
$this is defined (a)
$this is not defined.
$this is defined (b)
$this is not defined.
Example #2 绠鍗曠殑绫诲畾涔
<?php
class SimpleClass
{
// 鎴愬憳澹版槑
public $var = 'a default value';
// 鏂规硶澹版槑
public function displayVar() {
echo $this->var;
}
}
?>
Example #3 绫绘垚鍛樼殑榛樿鍊
<?php
class SimpleClass
{
// 鏃犳晥鐨勭被鎴愬憳瀹氫箟锛
public $var1 = 'hello '.'world';
public $var2 = <<<EOD
hello world
EOD;
public $var3 = 1+2;
public $var4 = self::myStaticMethod();
public $var5 = $myVar;
// 姝g‘鐨勭被鎴愬憳瀹氫箟锛
public $var6 = myConstant;
public $var7 = self::classConstant;
public $var8 = array(true, false);
}
?>
Note:
闄ゆ涔嬪杩樻湁涓嶅皯鐢ㄤ簬澶勭悊绫讳笌瀵硅薄鐨勫嚱鏁帮紝璇︽儏鍙傝 绫 / 瀵硅薄鍑芥暟
new
瑕佸垱寤轰竴涓璞$殑瀹炰緥锛屽繀椤诲垱寤轰竴涓柊瀵硅薄骞跺皢鍏惰祴缁欎竴涓彉閲忋傚綋鍒涘缓鏂板璞℃椂璇ュ璞℃绘槸琚祴鍊硷紝闄ら潪璇ュ璞″畾涔変簡鏋勯犲嚱鏁板苟涓斿湪鍑洪敊鏃舵姏鍑轰簡涓涓紓甯搞
Example #4 鍒涘缓涓涓疄渚媏
褰撴妸涓涓璞″凡缁忓垱寤虹殑瀹炰緥璧嬬粰涓涓柊鍙橀噺鏃讹紝鏂板彉閲忎細璁块棶鍚屼竴涓疄渚嬶紝灏卞拰鐢ㄨ瀵硅薄璧嬪间竴鏍枫傛琛屼负鍜岀粰鍑芥暟浼犻掑叆瀹炰緥鏃朵竴鏍枫傚彲浠ョ敤鍏嬮殕缁欎竴涓凡鍒涘缓鐨勫璞″缓绔嬩竴涓柊瀹炰緥銆
Example #5 瀵硅薄璧嬪
<?php
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
浠ヤ笂渚嬬▼浼氳緭鍑猴細
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
extends
涓涓被鍙互鍦ㄥ0鏄庝腑鐢 extends 鍏抽敭瀛楃户鎵垮彟涓涓被鐨勬柟娉曞拰鎴愬憳銆備笉鑳芥墿灞曞涓被锛屽彧鑳界户鎵夸竴涓熀绫汇
琚户鎵跨殑鏂规硶鍜屾垚鍛樺彲浠ラ氳繃鐢ㄥ悓鏍风殑鍚嶅瓧閲嶆柊澹版槑琚鐩栵紝闄ら潪鐖剁被瀹氫箟鏂规硶鏃朵娇鐢ㄤ簡 final 鍏抽敭瀛椼傚彲浠ラ氳繃 parent:: 鏉ヨ闂瑕嗙洊鐨勬柟娉曟垨鎴愬憳銆
Example #6 绠鍗曠殑绫荤户鎵
<?php
class ExtendClass extends SimpleClass
{
// Redefine the parent method
function displayVar()
{
echo "Extending class ";
parent::displayVar();
}
}
$extended = new ExtendClass();
$extended->displayVar();
?>
浠ヤ笂渚嬬▼浼氳緭鍑猴細
Extending class
a default value
姣忎釜绫荤殑瀹氫箟閮戒互鍏抽敭瀛 class 寮澶达紝鍚庨潰璺熺潃绫诲悕锛屽彲浠ユ槸浠讳綍闈 PHP 淇濈暀瀛楃殑鍚嶅瓧銆傚悗闈㈣窡鐫涓瀵硅姳鎷彿锛岄噷闈㈠寘鍚湁绫绘垚鍛樺拰鏂规硶鐨勫畾涔夈備吉鍙橀噺 $this 鍙互鍦ㄥ綋涓涓柟娉曞湪瀵硅薄鍐呴儴璋冪敤鏃朵娇鐢ㄣ$this 鏄竴涓埌璋冪敤瀵硅薄锛堥氬父鏄柟娉曟墍灞炰簬鐨勫璞★紝浣嗕篃鍙互鏄彟涓涓璞★紝濡傛灉璇ユ柟娉曟槸浠庣浜屼釜瀵硅薄鍐呴潤鎬佽皟鐢ㄧ殑璇濓級鐨勫紩鐢ㄣ傜湅涓嬮潰渚嬪瓙锛
Example #1 闈㈠悜瀵硅薄璇█涓殑 $this 鍙橀噺
<?php
class A
{
function foo()
{
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ") ";
} else {
echo "$this is not defined. ";
}
}
}
class B
{
function bar()
{
A::foo();
}
}
$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();
?>
浠ヤ笂渚嬬▼浼氳緭鍑猴細
$this is defined (a)
$this is not defined.
$this is defined (b)
$this is not defined.
Example #2 绠鍗曠殑绫诲畾涔
<?php
class SimpleClass
{
// 鎴愬憳澹版槑
public $var = 'a default value';
// 鏂规硶澹版槑
public function displayVar() {
echo $this->var;
}
}
?>
Example #3 绫绘垚鍛樼殑榛樿鍊
<?php
class SimpleClass
{
// 鏃犳晥鐨勭被鎴愬憳瀹氫箟锛
public $var1 = 'hello '.'world';
public $var2 = <<<EOD
hello world
EOD;
public $var3 = 1+2;
public $var4 = self::myStaticMethod();
public $var5 = $myVar;
// 姝g‘鐨勭被鎴愬憳瀹氫箟锛
public $var6 = myConstant;
public $var7 = self::classConstant;
public $var8 = array(true, false);
}
?>
Note:
闄ゆ涔嬪杩樻湁涓嶅皯鐢ㄤ簬澶勭悊绫讳笌瀵硅薄鐨勫嚱鏁帮紝璇︽儏鍙傝 绫 / 瀵硅薄鍑芥暟
new
瑕佸垱寤轰竴涓璞$殑瀹炰緥锛屽繀椤诲垱寤轰竴涓柊瀵硅薄骞跺皢鍏惰祴缁欎竴涓彉閲忋傚綋鍒涘缓鏂板璞℃椂璇ュ璞℃绘槸琚祴鍊硷紝闄ら潪璇ュ璞″畾涔変簡鏋勯犲嚱鏁板苟涓斿湪鍑洪敊鏃舵姏鍑轰簡涓涓紓甯搞
Example #4 鍒涘缓涓涓疄渚媏
1
2
3
|
<?php
$instance = new SimpleClass();
?>
|
Example #5 瀵硅薄璧嬪
<?php
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
浠ヤ笂渚嬬▼浼氳緭鍑猴細
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
extends
涓涓被鍙互鍦ㄥ0鏄庝腑鐢 extends 鍏抽敭瀛楃户鎵垮彟涓涓被鐨勬柟娉曞拰鎴愬憳銆備笉鑳芥墿灞曞涓被锛屽彧鑳界户鎵夸竴涓熀绫汇
琚户鎵跨殑鏂规硶鍜屾垚鍛樺彲浠ラ氳繃鐢ㄥ悓鏍风殑鍚嶅瓧閲嶆柊澹版槑琚鐩栵紝闄ら潪鐖剁被瀹氫箟鏂规硶鏃朵娇鐢ㄤ簡 final 鍏抽敭瀛椼傚彲浠ラ氳繃 parent:: 鏉ヨ闂瑕嗙洊鐨勬柟娉曟垨鎴愬憳銆
Example #6 绠鍗曠殑绫荤户鎵
<?php
class ExtendClass extends SimpleClass
{
// Redefine the parent method
function displayVar()
{
echo "Extending class ";
parent::displayVar();
}
}
$extended = new ExtendClass();
$extended->displayVar();
?>
浠ヤ笂渚嬬▼浼氳緭鍑猴細
Extending class
a default value
浠ヤ笂灏辨槸PHP鐨勫熀鏈蹇电殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒