OOP PHP-从一个类中获取变量并将其放入另一个文件中的类中

OOP PHP-从一个类中获取变量并将其放入另一个文件中的类中,php,class,oop,Php,Class,Oop,我有这样一种情况:我正在创建一个控制器文件,其中echo的json输出将在客户端与ajax一起使用: echo json_encode($response) 另一个文件中的主类从CMS中获取所有设置变量 现在,控制器文件有一个从API生成请求的类,但是类(username、id、count等)中的设置变量是硬编码的,因为我不知道如何准确地从另一个文件中的主类获取它们。通过硬编码设置,控制器文件将按预期创建并回显json输出。它只需要来自主类的动态变量 请原谅我缺乏OOP的知识和用法。我一直在尝试

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

另一个文件中的主类从CMS中获取所有设置变量

现在,控制器文件有一个从API生成请求的类,但是类
(username、id、count等)
中的设置变量是硬编码的,因为我不知道如何准确地从另一个文件中的主类获取它们。通过硬编码设置,控制器文件将按预期创建并回显json输出。它只需要来自主类的动态变量

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

**编辑**根据作者的评论重新思考一下这一点,因为它更有意义。因此,如果我将api_请求函数移动到mainClass中,并
返回
响应,我就可以得到所需的变量,请求仍然有效。因此,我会问——我怎么还能在一个单独的文件中回显来自api_请求函数的
$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;
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;

您可以使用
this
从当前对象访问属性。这也适用于从父类继承的属性

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;
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;

您可以使用
this
从当前对象访问属性。这也适用于从父类继承的属性

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;
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;

既然你要我指出这一点

您的体系结构中有很多缺陷

首先

当你这样做的时候

class apiCall扩展了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)您可以将该数组事件转换为
YAML
XML
,而不仅仅是
json

而且,在您的情况下,您应该避免继承。编写处理APICALL的单个类。因此,它看起来像

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');

  }
}

既然你要我指出这一点

您的体系结构中有很多缺陷

首先

当你这样做的时候

class apiCall扩展了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)您可以将该数组事件转换为
YAML
XML
,而不仅仅是
json

而且,在您的情况下,您应该避免继承。编写处理APICALL的单个类。因此,它看起来像

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');

  }
}

一句话:您正在破坏
SRP
LSP
api\u请求()
应该不发送绑定(不回显,而是返回)因为它会导致该方法同时做两件事,这是一种不好的做法。这在@DaveJust是非常有意义的。请参阅我的上面的编辑,因为处理新问题可能更容易、更好一些…?底线:您正在打破
SRP
LSP
api\u request()
应该执行绑定而不是发送(不回显,返回)因为它会导致该方法同时做两件事,这是一种不好的做法。这在@DaveJust是非常有意义的。请参阅我上面的编辑,因为处理新问题可能更容易、更好……使用您的示例,重新构建它,一切都按预期进行了!非常感谢您抽出时间写出来并指出这一点缺陷。还有很多东西需要学习。@RyanPalmer有很多好的初学者教程:
www.phpmaster.com
使用您的示例,重新构建它,一切都如预期的那样工作!非常感谢您花时间写出来并指出缺陷。还有很多东西需要学习。@RyanPalmer有很多好的初学者教程:
www.phpmaster.com