您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP种继承

2024/3/6 18:00:30发布34次查看
php类继承
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
fieldid=$fieldid;
$this->fieldname=$fieldname;
$this->fieldcaption=$fieldcaption;
$this->isempty=$isempty;
$this->validatescript=$validatescript;
$this->validatefailedmessage=$validatefailedmessage;
?}
}
?>
这是一个简单的clsfield类,包含了一些基本属性…
下面演示其中一个派生类clsreffield
reffield.php
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
你通过在浏览器输入 http://localhost/urproject/debug.php就可以看到效果的了。
?
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product