2013-04-06 29 views
0

首先,我几乎是一个noopie关于oop等,所以请善待。oop,php,扩展类,运行函数,当具体的方法在课堂上被称为

我也读了很多的手册,但不知在所有的东西,迷路了,找不到答案:( 所以任何提示,链接等将是非常赞赏

反正我发现自己在一个。情况下,我需要调用函数时被执行的一类特定的方法。然而,我不能碰类本身

证明,类是这样的:

/* icannot touch this class*/ 
class Bar extends Foo { 
/*some other methods and variables here, omitted for sanity reasons**/ 

function DBSave(){  

    $this->cat_parent = intval($this->cat_parent); 
    $this->cat_num_files = intval($this->cat_num_files); 

    return parent::DBSave(); 
} 

/*some more methods omitted for sanity reasons**/ 
} 
/*end of class*/ 

/** 编辑 ***/

基于下面我现在写的评论/添加以下,但它并没有真正做的事情我曾经希望的方式:(

class Baz extends Bar { 
    public function DBSave() { 
     /*here I want to use $this->cat_parent etc to do something with*/ 
     parent::DBSave(); 
    } 
} 
/*this is probably also wrong, but I tried with the following results*/ 
$b = new Baz();/*just adding this does nothing*/ 
$b->DBSave();/* when this is added as well it *always* runs regrdless of whether bar->foo is called*/ 

还 - 我忘了提及,对不起 - 在Baz中的方法需要变量($ this-> cat_parent等)到一些合理的 希望以上是有道理的......?

/**编辑结束* **/

每当DBSave被调用,我想运行其他功能,但我不知道如何去说:( 我尝试了一些扩展类的东西,但它只是错了或一直呼吁它等...任何提示将不胜感激

当然高兴地扩大这个问题,如果需要

感谢

+0

你想打什么方法? $这个 - >方法? parent :: method()? – bwoebi 2013-04-06 14:23:16

+0

如果您像这样创建扩展类,则需要确保现有代码实例化您的新类而不是原始类。 – DCoder 2013-04-06 14:30:03

+0

你这样做完全错了。你完全失去了数据库抽象和混合的概念。你实际上需要实现一个DataMapper来避免像这样的问题。再次 - 你在做什么不是这样的事情是如何实施的 – Yang 2014-03-04 05:56:38

回答

1

最简单的方法很可能会做的事:

class Baz extends Bar { 
    public function DBSave() { 
     parent::DBSave(); 
    } 
} 

您可以在方法体中定义的附加功能。

+0

我会给它一个去。感谢您的快速回复 – olly 2013-04-06 14:28:38

+0

嗯,类型的作品达到一个点,我会做一些编辑原始问题 – olly 2013-04-06 14:52:45