2013-07-26 3 views
1

나는 PHP 언어의 더 나은 실천에 대한 답을 찾고있다. 나는 다음과 같은 코드를 가지고있다. 표시를 위해 출력 함수에 매개 변수 목록을 보내는 것이 더 좋습니까? 아니면 표시 할 출력 함수에 대한 배열을 형성해야합니까? 의견이나 대답이 있으면 답변을 뒷받침하는 모든 문서를 제공하여 나 자신을 포함한 누구나 향후이 자료를 볼 수 있습니다. 나는 출력기 기능을 포함하여 브라우져에 메시지를 출력하는 방법을 보여 주었지만, flashdata를 사용하여 디스플레이에 표시하는 방법을 고려해 보았습니다. 그러나 내가 정보, 경고, 성공 메시지 등 다양한 유형의 메시지를 사용할 때 어떻게 사용하는지 궁금합니다.배열과 함수 호출에 대한 더 나은 연습

if (!$this->form_is_valid()) 
{ 
    $this->output('The form did not validate successfully!', 
        'Form Not Validated', 'Error'); 
    return; 
} 

public function output($message, $title, $status) 
{ 
    switch (strtoupper($status)) 
    { 
     default: 
     case 'ERROR': 
      $status = 'Error'; 
      break; 
     case 'NOTICE': 
      $status = 'Notice'; 
      break; 
     case 'SUCCESS': 
      $status = 'Success'; 
      break; 
    } 
    $this->output->set_content_type('application/json')->set_output(json_encode(array 
     (
     'output_status' => $status, 
     'output_title' => $title, 
     'output_message' => $message))); 
} 
+0

반환이 필요하지 않습니다. form_is_valid 또는 출력 메소드의 정의가 없으면 의미있는 평가를 내리기가 어렵거나 불가능합니다. – Orangepill

+0

가능한 답변을 제공하기 위해 답을 업데이트했습니다. – user2576961

+0

이것은 유효하지 않은 것으로 보입니다. '$ this-> output'은 함수로 정의되어 있지만 그것에 대한 메소드를 호출합니다'$ this-> output-> set_content_type' – Orangepill

답변

2

더 나은 옵션은 출력 방법으로 개체를 전달하는 것입니다 :

<?php 
interface Message 
{ 
    public function getContents(); 
    public function getSummary(); 
    public function getType(); 
} 

abstract class AbstractMessage() implements Message 
{ 
    protected $type; 
    protected $contents; 
    protected $summary; 

    protected function __construct($contents, $summary) { 
    $this->contents = $contents; 
    $this->summary = $summary; 
    } 

    public function getContents() { 
    return $this->contents; 
    } 

    public function getSummary() { 
    return $this->summary; 
    } 

    public function getType() { 
    return $this->type; 
    } 
} 

class ErrorMessage() extends AbstractMessage 
{ 
    public function __construct($contents, $summary) { 
    parent::__construct($contents, $summary); 
    $this->type = 'Error'; 
    } 
} 

class InfoMessage() extends AbstractMessage 
{ 
    ... 
} 

... 

if (!$this->form_is_valid()) 
{ 
    $this->output(new ErrorMessage(
    'The form did not validate successfully!', 
    'Form Not Validated', 
    'Error' 
)); 
    return; 
} 
관련 문제