扩展PHP辅助方法

时间:2016-02-14 14:06:07

标签: php oop

对于deleteBarDocument(),我希望不实施

$this->getModel()->updateParentAudit($this->audit_table,$_POST['id']);

deleteDocumentHelper()辅助方法中,但实现其他内容,例如

$this->bla($_POST['id']);
$this->blabla($_POST['id'],$_POST['doc_id']);

如何为deleteDocumentHelper()延长deleteBarDocument(),但保留deleteFooDocument()的原样?

通常,我会替换

$this->getModel()->updateParentAudit($this->audit_table,$_POST['id']);

$this->doSomething();

并使用所需的任何代码创建doSomething()方法。可能采用这种方法会影响deleteFooDocument()deleteBarDocument(),这是不可取的。

或者我应该通过辅助方法传递一个匿名函数?我谨慎这样做,因为我已经警告过应该谨慎使用匿名功能。

<?php
class parentController
{
    protected function deleteDocumentHelper($type){
        if(isset($_POST['id'],$_POST['doc_id'])){
            if(documents::removeDocument($type,$_POST['doc_id'],$_POST['id']))
            {
                $success=1;
                //Ability to replace the following line with one or more lines
                $this->getModel()->updateParentAudit($this->audit_table,$_POST['id']);
            }
            else {$success=0;}
            header('Content-Type: application/json;');
            $this->dontCache();
            echo(json_encode(array('success'=>$success)));
        }
        else {exit($this->missingPage());}        
    }
}  

class childController extends parentController
{
    public function deleteFooDocument(){$this->deleteDocumentHelper('foo');}
    public function deleteBarDocument(){$this->deleteDocumentHelper('bar');}
}

//Application creates childController object, and evokes apprpropriate method based on GET/POST  
?>

1 个答案:

答案 0 :(得分:0)

你说:

  

通常,我会替换

$this->getModel()->updateParentAudit($this->audit_table,$_POST['id']);
     

$this->doSomething();
     

并使用所需的任何代码创建doSomething()方法。可能采用这种方法会影响deleteFooDocument()deleteBarDocument(),这是不可取的。

但是,为什么不按照以下方式?

class parentController
{
    protected function doSomething()
    {
        $this->getModel()->updateParentAudit($this->audit_table,$_POST['id']);
    }

    protected function deleteDocumentHelper($type){
        if(isset($_POST['id'],$_POST['doc_id'])){
            if(documents::removeDocument($type,$_POST['doc_id'],$_POST['id']))
            {
                $success=1;
                //Ability to replace the following line with one or more lines
                $this->doSomething();
            }
            (...)
        }
        else {exit($this->missingPage());}        
    }
}  

class childController extends parentController
{
    private $somethingElse = False;

    protected function doSomething()
    {
        if( $this->somethingElse )
        {
            // Your deleteBar code 
        }
        else
        {
            parent::doSomething();
        }
    }

    protected function deleteDocumentHelper( $type, $somethingElse=False )
    {
        $this->somethingElse = $somethingElse;
        parent::deleteDocumentHelper( $type );
    }

    public function deleteFooDocument(){$this->deleteDocumentHelper('foo');}
    public function deleteBarDocument(){$this->$this->deleteDocumentHelper('bar',1);}
}

使用这种方法,您可以基本上保持不变的父类,并仅在需要时应用不同的代码。