2016-02-14 137 views
0

對於deleteBarDocument(),我希望不要執行擴展PHP輔助方法

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

,但實施其他的東西,如

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

如何爲deleteBarDocument()延長deleteDocumentHelper(),但讓它保持原樣deleteFooDocument()

通常情況下,我只想用

$this->doSomething(); 

更換

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

,並創建一個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 
?> 

回答

0

你說:

通常情況下,我只想用

$this->doSomething(); 

更換

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

,並創建一個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);} 
} 

通過這種方法,您可以實質上保留父類不變,並且只在需要時應用不同的代碼。

+0

感謝融合。是的,我可以這樣做,但它看起來並不真實。我不是說你的解決方案不乾淨,但我所做的不是。我可能會這樣做倒退... – user1032531

+0

其實,你想擴展'parentController-> deleteDocumentHelper'或者修改自己的代碼嗎? – fusion3k