PHP种继承

WBOY
Release: 2016-06-13 13:01:55
Original
775 people have browsed it

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就可以看到效果的了。

?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!