我有一个容器类:
class ContainerClass {
var $subobj;
var $myName;
function __construct($val, $name)
{
$this->myName = $name;
$this->subobj = new ItemClass ($val);
}
function getItem()
{
return ($this->subobj);
}
}
和一个班级:
class ItemClass {
var $value;
function __construct($val)
{
$this->value = $val;
}
}
这两个类是假设库的成员,只需使用include
调用即可在许多项目中使用。
在我的项目中,我需要做一些特定的显示,所以对于每个项目,我想在不修改库源代码的情况下添加类似这样的方法(具体项目)内容):
ContainerClass
:function getDisplay () {
return ($myName . " : " . $this->subobj->getDisplay());
}
ItemClass
:function getDisplay () {
return (/* some display */ $this->value);
}
function getDisplayAlone () {
return (/* some display */"<DIV>" . $this->value . "</DIV>");
}
function getDisplay () {
if ($this->value == 5) return ($myName . " : " . $this->subobj->getDisplay());
return ("No luck !");
}
ItemClass
:function getDisplay () {
return (/* some display */"Go to hell !");
}
function getDisplayAlone () {
return (/* some display */"<DIV>What's up doc ?</DIV>");
}
并且能够从每个项目的主要php文件中调用这些文件:
include "mylibrary.php";
$obj = new ContainerClass (10, "Me");
$obj->getDisplay();
$obj->getItem()->getDisplayAlone();
我发现了许多向类和对象添加方法的技巧和窍门,但它们要么不能在php5上工作,要么不允许在方法中使用$this
,要么在所有
有人可以帮助我实现这个目标吗?
答案 0 :(得分:0)
使用extends
class ProjectItemClass extends ContainerClass {
function getDisplay () {
return (/* some display */ $this->value);
}
function getDisplayAlone () {
return (/* some display */"<DIV>" . $this->value . "</DIV>");
}
}
class ProjectContainerClass extends ContainerClass {
public function __construct($val, $name) {
$this->myName = $name;
$this->subobj = new ProjectItemClass ($val);
}
function getDisplay () {
return ($myName . " : " . $this->subobj->getDisplay());
}
}
class ProjectItemClass extends ContainerClass {
function getDisplay () {
return (/* some display */"Go to hell !");
}
function getDisplayAlone () {
return (/* some display */"<DIV>What's up doc ?</DIV>");
}
}
class ProjectContainerClass extends ContainerClass {
public function __construct($val, $name) {
$this->myName = $name;
$this->subobj = new ProjectItemClass ($val);
}
function getDisplay () {
if ($this->value == 5) return ($myName . " : " . $this->subobj->getDisplay());
return ("No luck !");
}
}
确保在原始类之后包含/要求这些新类,然后像这样使用它们:
$obj = new ProjectContainerClass (10, "Me");
$obj->getDisplay();
$obj->getItem()->getDisplayAlone();
请注意重写__construct
以确保使用ProjectItemClass
代替ItemClass
。您还可以使用parent
调用来确保仍然使用对扩展类构造的任何更改(但$this->subobj
只会被调用两次。)
public function __construct($val, $name) {
parent::__construct($val, $name);
$this->subobj = new ProjectItemClass($val);
}
答案 1 :(得分:0)
假设这将是你的图书馆类:
class ContainerClass
{
protected $subobj; //Must be protected to be accessable for the child classes
protected $myName; //Must be protected to be accessable for the child classes
function __construct($val, $name)
{
$this->myName = $name;
$this->subobj = new ItemClass ($val);
}
function getItem()
{
return ($this->subobj);
}
}
class ItemClass {
private $value;
function __construct($val)
{
$this->value = $val;
}
}
如果要为每个项目实现不同的功能,则需要覆盖libary类。所以例如Project1ItemClass
扩展了libary ItemClass
类。这是一个例子:
class Project1ItemClass extends ItemClass {
function getDisplay () {
return (/* some display */ $this->value);
}
function getDisplayAlone () {
return (/* some display */"<DIV>" . $this->value . "</DIV>");
}
}
class Project1ContainerClass extends ContainerClass {
//Override the constructor cause you want to create a project 1 specific ItemClass
function __construct($val, $name)
{
$this->myName = $name;
$this->subobj = new Project1ItemClass ($val);
}
function getDisplay () {
return ($this->myName . " : " . $this->subobj->getDisplay());
}
}
以下示例将打印:qwe : asd
$Project1ContainerClass = new Project1ContainerClass("asd","qwe");
echo $Project1ContainerClass->getDisplay();
请注意,您应该在项目特定ContainerClass
中覆盖构造函数,因为您正在其中创建ItemClass
的新实例。如果您不重写构造函数,则创建ItemClass
的实例,而您的意图是Project1ItemClass
的实例。