0

我有一种情况,我正在创建一个控制器文件,该文件回显的 json 输出将在客户端与 ajax 一起使用: echo json_encode($response);

除其他外,另一个文件中的主类从 CMS 中获取所有设置变量。

现在,控制器文件有一个从 API 生成请求的类,但是类中的设置变量(username, id, count, etc.)是硬编码的,因为我无法弄清楚如何从另一个文件的主类中准确获取它们。通过硬编码设置,控制器文件按预期创建和回显 json 输出。它只需要主类中的动态变量。

请原谅我缺乏 OOP 的知识和用法。我一直在尝试使用这样的结构,再次尝试将用户名和其他变量从主类获取到单独文件中的另一个类中。

**编辑** 根据@Dave的评论重新思考这一点,因为它更有意义。因此,如果我将 api_request 函数移动到 mainClass 和return响应中,我可以获得我需要的变量并且请求仍然有效。所以这会让我问 - 我怎样才能$response在单独的文件中回显来自 api_request 函数的?那个带有 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;
4

2 回答 2

3

既然你要我指出这一点,

你的架构有很多缺陷

首先

当你这样做时,

class apiCall extends mainClass {

你同时打破了单一职责原则和里氏替换原则

控制器不应该显任何东西

MVC 本身看起来像

$modelLayer = new ModelLayer();

$view = new View($modelLayer);

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

echo $view->render();

您实际上实现了接近Model-View-Presenter 的东西,而不是MVC

第三

由于您的课程从那时开始,api..因此无需在方法中包含该名称。

您不必json_encode()与生成逻辑紧密耦合。该方法应该只返回一个数组,然后你会json_encode()得到那个数组。好处?1)关注点分离 2)您可以将该数组事件转换为YAMLor XML,不仅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');

  }
}
于 2013-06-29T21:55:03.567 回答
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;
于 2013-06-29T19:02:03.200 回答