2013-06-29 22 views
0

我已經在那裏我創建呼應的JSON輸出控制器文件的情況中獲取增值經銷商使用的客戶端使用Ajax: echo json_encode($response);OOP PHP - 從一類到類另一個文件

的在另一個文件中的主類,除其他外,抓住了CMS的所有設置變量。

現在,控制器文件有一個類可以生成來自API的請求,但類(username, id, count, etc.)中的設置變量是硬編碼的,因爲我無法弄清楚如何從另一個主類中獲取它們文件。使用硬編碼設置,控制器文件按預期創建並回顯json輸出。它只需要主類的動態變量。

請原諒我缺乏OOP的知識和使用。我一直在用像這樣的結構來嘗試它,在這裏再次嘗試從主類獲取用戶名和其他變量到單獨文件中的另一個類。

** 編輯 **根據@Dave Just的評論重新考慮這一點,因爲它更合理。因此,如果我將api_request函數移到mainClass和return響應中,我可以得到我需要的變量,並且請求仍然有效。所以這將導致我問 - 我怎麼能仍然在單獨的文件中從api_request函數回顯$response?與json分開的文件是我用於我的ajax腳本。

class mainClass { 
    public $username; 

    function __construct() { 
     ... 
    } 

    public function api_settings($username) { 
     ... 
    } 
} 

$main_class = new mainClass; 
$main_class->api_settings(); 
// OR 
$main_class->username; 

API-call.php

class apiCall extends mainClass { 

    public $username; 

    function __construct() { 
     parent::__construct; 
     ... 
    } 

    public function api_request() { 
     ... 

     $return = $api_auth->request(
      'GET', 
      $api_auth->url('/cms-plug'), 
      array(
       //where I need to be able to grab the $username from the main class 
       'username' => 'joebob' 
      ) 
     ); 

     echo json_encode($response); 
    } 

} 

$api_class = new apiCall; 
+0

底線:你打破了'SRP'和'LSP' – Yang

+0

和'api_request()'應該做綁定不發送(不要回應它,但返回),因爲它導致該方法一次做2件事,這是不好的做法。 – Yang

+0

這很有道理@DaveJust。看到我上面的編輯,因爲它可能更容易,更好的做法來處理新的問題......? –

回答

3

既然你問我指出這一點,

有在你的架構這麼多破綻

首先

當你不喜歡它,

class apiCall extends mainClass {

你打破單一職責原則里氏替換原則同時

控制器應從未呼應什麼

MVC本身看起來像

$modelLayer = new ModelLayer(); 

$view = new View($modelLayer); 

$controller = new Controller($modelLayer); 
$controller->indexAction($request); 

echo $view->render(); 

您實際執行的東西是接近模型 - 視圖 - 演示,不MVC

由於類從api..開始那麼就沒有必要在方法的這個名字。

您不必緊密結合json_encode()與生成邏輯。該方法應該只返回一個數組,然後你會json_encode()那個數組。好處? 1)分離的擔憂2)你可以事件轉換該陣列YAMLXML,不僅JSON

而且,你應該避免在你的情況下繼承。編寫處理ApiCalls的單數類。因此,它看起來就像是,

final class ApiCall 
{ 

    /** 
    * I'd use a name that makes sense 
    * 
    * @param string $username 
    * @return array on success, FALSE on failure 
    */ 
    public function fetchByUsername($username) 
    { 

     $return = $api_auth->request(
      'GET', 
      $api_auth->url('/cms-plug'), 
      array('username' => $username) 
     ); 

     if ($response !== false){ 

      return $response; 

     } else { 

      return false; 
     } 
    } 
} 

而且你會使用它像,

if (isset($_GET['username'])){ 

    $api = new ApiCall(); 

    $result = $api->fetchByUsername($_GET['username']); 

    if ($result !== false){ 

    // Respond as JSON 
    die(json_encode($result)); 

    } else { 

    die('Wrong username'); 

    } 
} 
+0

使用你的例子,重建它,一切都按預期工作!真的很感激你花時間寫出來並指出缺陷。還有很多要學習。 –

+0

@RyanPalmer初學者有很好的教程:'www.phpmaster.com' – Yang

1

您可以從當前對象與this訪問性能。這也適用於父類的繼承屬性。

api-call。PHP

class apiCall extends mainClass { 
    //public $username; // you don't have to decalre $username again, it gets already inherited from mainClass since its public there 
    function __construct() { 
     parent::__construct; 
     ... 
    } 

    public function api_request() { 
     ... 
     $return = $api_auth->request(
      'GET', 
      $api_auth->url('/cms-plug'), 
      array(
       //where I need to be able to grab the $username from the main class 
       'username' => this->username // vars of the current object and inherited vars are available with "this" 
      ) 
     ); 
     echo json_encode($response); 
    } 
} 
$api_class = new apiCall;