PHP鎶借薄绫
鎶借薄绫
PHP5鏀寔鎶借薄绫诲拰鎶借薄鏂规硶銆傛娊璞$被涓嶈兘鐩存帴琚疄渚嬪寲锛屼綘蹇呴』鍏堢户鎵胯鎶借薄绫伙紝鐒跺悗鍐嶅疄渚嬪寲瀛愮被銆傛娊璞$被涓 鑷冲皯瑕佸寘鍚竴涓娊璞℃柟娉曘傚鏋滅被鏂规硶琚0鏄庝负鎶借薄鐨勶紝閭d箞鍏朵腑灏变笉鑳藉寘鎷叿浣撶殑鍔熻兘瀹炵幇銆
缁ф壙涓涓娊璞$被鐨勬椂鍊欙紝瀛愮被蹇呴』瀹炵幇鎶借薄绫讳腑鐨勬墍鏈夋娊璞℃柟娉曪紱鍙﹀锛岃繖浜涙柟娉曠殑鍙鎬 蹇呴』鍜屾娊璞$被涓竴鏍凤紙鎴栬呮洿涓哄鏉撅級銆傚鏋滄娊璞$被涓煇涓娊璞℃柟娉曡澹版槑涓簆rotected锛岄偅涔堝瓙绫讳腑瀹炵幇鐨勬柟娉曞氨搴旇澹版槑涓簆rotected鎴栬卲ublic锛岃屼笉 鑳藉畾涔変负private銆
Example #1 鎶借薄绫讳唬鐮佺ず渚
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<span style="font-size:14px;"><code><?php
abstract class AbstractClass
{
// 寮哄埗瑕佹眰瀛愮被瀹氫箟杩欎簺鏂规硶
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// 鏅氭柟娉曪紙闈炴娊璞℃柟娉曪級
public function printOut() {
print $this->getValue() . " ";
}
}
class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
return "ConcreteClass1";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass1";
}
}
class ConcreteClass2 extends AbstractClass
{
public function getValue() {
return "ConcreteClass2";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass2";
}
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ." ";
$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_') ." ";
?></code></span>
|
浠ヤ笂渚嬬▼浼氳緭鍑猴細
1
2
3
4
|
<span style="font-size:14px;">ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2</span>
|
浠ヤ笂灏辨槸PHP鎶借薄绫荤殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒