欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php面向?qū)ο蟮姆椒ㄖ剌d兩種版本比較

 更新時間:2008年09月08日 21:26:29   作者:  
“重載”是類的多態(tài)的一種實(shí)現(xiàn)。函數(shù)重載指一個標(biāo)識符被用作多個函數(shù)名,且能夠通過函數(shù)的參數(shù)個數(shù)或參數(shù)類型將這些同名的函數(shù)區(qū)分開來,調(diào)用不發(fā)生混淆。這樣做的主要好處就是,不用為了對不同的參數(shù)類型或參數(shù)個數(shù),而寫多個函數(shù)。
多個函數(shù)用同一個名字,但參數(shù)表,即參數(shù)的個數(shù)或(和)數(shù)據(jù)類型可以不同,調(diào)用的時候,雖然方法名字相同,但根據(jù)參數(shù)表可以自動調(diào)用對應(yīng)的函數(shù)。

PHP4 中僅僅實(shí)現(xiàn)了面向?qū)ο蟮牟糠值?、簡單的功能,?PHP5 以后對對象的支持就強(qiáng)大的多了。

對于多態(tài)的實(shí)現(xiàn),PHP4 只支持覆蓋(override),而不支持重載(overload)。但我們可以通過一些技巧來“模擬”重載的實(shí)現(xiàn)。

PHP5 雖然可以支持覆蓋和重載,但重載在具體實(shí)現(xiàn)上,和其他語言還有較大的差別。

1,在 PHP4 中“模擬”重載

試看以下代碼:

<?php
//根據(jù)參數(shù)個數(shù)選擇執(zhí)行不同的方法(在 PHP4 中模擬"重載"(多態(tài)的一種)

class Myclass
{
function Myclass()
{
$method = "method" . func_num_args();
$this->$method();
}

function method1($x)
{
echo "method1";
}

function method2($x, $y)
{
echo 'method2';
}
}

//通過在類中的額外的處理,使用這個類對用戶是透明的:
$obj1 = new Myclass('A'); //將調(diào)用 method1
$obj2 = new Myclass('B','C'); //將調(diào)用 method2
?>

以上代碼中,通過在構(gòu)造函數(shù)中使用 func_num_args() 函數(shù)取到參數(shù)的個數(shù),自動執(zhí)行 method1 或 method2 方法。我們可以結(jié)合函數(shù) func_get_arg(i) 和 func_get_args() 對以上示例進(jìn)行改進(jìn)。

2,在 PHP5 中使用重載

先看以下示例:

復(fù)制代碼 代碼如下:

<?php
class Myclass
{
public $attriable;
public $one = "this is one";
public $two = "this is two";

function __construct()
{
}

function one($one)
{
$this->one=$one;
$this->attriable = $this->one;
}

function one($one, $two)
{
$this->one=$one;
$this->two=$two;
$this->attriable = $this->one . $this->two;
}

function display()
{
echo $this->attriable;
}
}

$one = "this is my class";
$two = "Im the best";
$myclass = new myclass();
$myclass->one($one);
$myclass->display();

$myclass->one($one, $two);
$myclass->display();

//本例的做法,在 PHP 中是不正確的!
?>


使用過 C++、Java、C# 重載的人,很習(xí)慣地就會寫出以上的重載實(shí)現(xiàn)的 PHP 代碼。但這在 PHP5 中是不正確的。PHP5 并不是對前述幾種語言的模仿,而是有自己的一套實(shí)現(xiàn)方法重載的方法(是好是壞,這里不討論)。 雖說 PHP5 的類較 PHP4 強(qiáng)大了許多,但是在“重載”這個問題上并沒有像我們預(yù)期的那樣“改善”。在“強(qiáng)”類型的語言中可以通過不同的參數(shù)類型來實(shí)現(xiàn)“重載”,比如C++、Java、C# 等。在“固定參數(shù)”傳遞的語言中,還可以通過參數(shù)的個數(shù)進(jìn)行傳遞,比如 Java,但是 PHP 是弱類型語言,因此不會出現(xiàn)類似以上的“重載”。

PHP5 中重載可以通過 __get, __set, and __call 幾個特殊方法來進(jìn)行。當(dāng) Zend 引擎試圖訪問一個成員并沒有找到時,PHP將會調(diào)用這些方法。

在以下示例中,__get和__set代替所有對屬性變量數(shù)組的訪問。如果必要,你還可以實(shí)現(xiàn)任何類型你想要的過濾。例如,腳本可以禁止設(shè)置屬性值, 在開始時用一定的前綴或包含一定類型的值。__call 方法說明了你如何調(diào)用未經(jīng)定義的方法。你調(diào)用未定義方法時,方法名和方法接收的參數(shù)將會傳給__call方法, PHP傳遞__call的值返回給未定義的方法。

復(fù)制代碼 代碼如下:

<?php
class Overloader
{
private $properties = array();

function __get($property_name)
{
if(isset($this->properties[$property_name]))
{
return($this->properties[$property_name]);
}
else
{
return(NULL);
}
}

function __set($property_name, $value)
{
$this->properties[$property_name] = $value;
}

public function __call($method, $p)
{
print("Invoking $method()<br>\n");
//print("Arguments: ");
//print_r($args);
if($method == 'display')
{
if(is_object($p[0]))
$this->displayObject($p[0]);
else
if(is_array($p[0]))
$this->displayArray($p[0]);
else
$this->displayScalar($p[0]);
}
}

public function displayObject($p)
{
echo ("你傳入的是個對象,內(nèi)容如下:<br>");
print_r($p);
echo "<hr>";
}

public function displayArray($p)
{
echo ("你傳入的是個數(shù)組,內(nèi)容如下:<br>");
print_r($p);
echo "<hr>";
}

public function displayScalar($p)
{
echo ("你傳入的是個單獨(dú)變量,內(nèi)容如下:<br>" . $p);
echo "<hr>";
}
}

$o = new Overloader();

//調(diào)用 __set() 給一個不存在的屬性變量賦值
$o->dynaProp = "Dynamic Content";

//調(diào)用 __get()
print($o->dynaProp . "<br>\n");

//調(diào)用 __call()
//$o->dynaMethod("Leon", "Zeev");

$o->display(array(1,2,3));
$o->display('Cat');
?>


以上代碼中,調(diào)用了 display() 方法,可以根據(jù)參數(shù)的類型和個數(shù)調(diào)用類中的對應(yīng)的代碼段,從而實(shí)現(xiàn)了對象方法的重載。

相關(guān)文章

最新評論