PHP種繼承

來源:互聯網
上載者:User
PHP類繼承

class ClsPerson
{
?var $personID;
?var $personName;
?function ClsPerson($personID,$personName)
?{
??//$this->personID=$personID;
??//$this->personName=$personName;
??echo "";
?}
}

class ClsStudent extends ClsPerson
{
?var $cardID;
?function ClsStudent($personID ,$personName,$cardID)
?{
??parent::ClsPerson($personID,$personName);
??echo "";
?}
}

$student = new ClsStudent("3234","DEXTERLESLIE","440402");
?>

?

?

?

?

?

?

?

?

?

?

?

?

打算使用一個很簡單的實際例子示範PHP的簡單物件導向
在實際編寫Web時候,我們經常遇到 ,似乎我們都比較討厭這些重複性的東西,那麼我們可以嘗試把這些具有共同特徵的東西封裝好,然後傳入動態變化的參數就可以重複使用了,並且這重複性的東西完全可以按照實際開發應用過程中的實際情況進行適當的調整。
現在設想,我設計一個很醜陋的基類 ClsField,就是所有表單域的基類,然後根據需求可以擴充ClsField,如ClsTextField,ClsNumberField,ClsRefField(參照域),以後我們只要new 這些class後就可以給我們產生

Field.php
//系統Field資料模型
class ClsField
{
?var $fieldID;
?var $fieldName;
?var $fieldCaption;
?//域是否允許空
?var $isEmpty;
?//javascript驗證邏輯
?var $validateScript;
?//驗證失敗提示資訊
?var $validateFailedMessage;
?
?function ClsField($fieldID,$fieldName,$fieldCaption,$isEmpty,
??$validateScript,$validateFailedMessage)
?{
??$this->fieldID=$fieldID;
??$this->fieldName=$fieldName;
??$this->fieldCaption=$fieldCaption;
??$this->isEmpty=$isEmpty;
??$this->validateScript=$validateScript;
??$this->validateFailedMessage=$validateFailedMessage;
?}
}
?>

這是一個簡單的ClsField類,包含了一些基本屬性…

下面示範其中一個衍生類別ClsRefField
RefField.php
include_once("Field.php");

/*
*參照域
*參照域編號,參照域帶出屬性
*/
class ClsRefField extends ClsField
{
?var $arrFieldProperties;
?var $openUrl;
?function ClsRefField($fieldID,$fieldName,$fieldCaption,$isEmpty,
?$validateScript,$validateFailedMessage,$openUrl)
?{
??//調用父類建構函式
??parent::ClsField($fieldID,$fieldName,$fieldCaption,$isEmpty,
??$validateScript,$validateFailedMessage);
??$this->openUrl=$openUrl;
?}
?public function __toString()
?{
??$strScript=<<
SCRIPT;
??$strInput="fieldID}\" name=\"{$this->fieldID}\"/>";
??$str="{$this->fieldCaption}:{$strInput}fieldID}('{$this->openUrl}','{$this->fieldCaption}')\">{$strScript}";
??return $str;
?}
}
?>
這個類ClsRefField extends ClsField 就是從ClsField 類派生出來的,其建構函式function ClsRefField(…..){
parent::ClsField($fieldID,$fieldName,$fieldCaption,$isEmpty,
??$validateScript,$validateFailedMessage);
}調用了父類的建構函式,在這裡我重寫了他的__toString()方法讓這個類在轉換為字串的時候自動產生相應的Html代碼


調用例子:
Debug.php

include_once("../inc/global_fun.php");
include_once("TextField.php");
include_once("RefField.php");

$rfUserID=new ClsRefField("userID","userID","使用者編碼","false","","請輸入使用者編碼","../admin/class_picker.php?refFieldID=userID");
//error($rfUserID);
echo $rfUserID;
?>

你通過在瀏覽器輸入 http://localhost/urProject/debug.php就可以看到效果的了。

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.