【问题标题】:OOP PHP - Get vars from one class and into a class within another fileOOP PHP - 从一个类中获取变量并进入另一个文件中的类
【发布时间】:2013-06-29 18:53:39
【问题描述】:

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

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

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

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

** EDIT ** 根据@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;

【问题讨论】:

  • 底线:你打破了SRPLSP
  • 并且api_request() 应该做绑定而不是发送(不要回显它,而是返回),因为它会导致方法一次做两件事,这是一种不好的做法。
  • 这很有意义@DaveJust。请参阅上面的编辑,因为处理新问题可能更容易和更好的做法......?

标签: php class oop


【解决方案1】:

既然你让我指出这一点,

你的架构有很多缺陷

第一

当你喜欢的时候,

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) 您可以将该数组事件转换为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');

  }
}

【讨论】:

  • 使用你的例子,重建它,一切都按预期工作!非常感谢您花时间写出来并指出缺陷。还有很多东西要学。
  • @RyanPalmer 有很好的初学者教程:www.phpmaster.com
【解决方案2】:

您可以使用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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2017-02-14
    • 2020-06-09
    相关资源
    最近更新 更多