Php 如何在没有函数手动返回值的情况下从另一个类初始化中检索值

Php 如何在没有函数手动返回值的情况下从另一个类初始化中检索值,php,class,oop,methods,Php,Class,Oop,Methods,我不知道该怎么办 我有两门课,LairEngine和RespondEngine RespondEngine中包含某些方法,这些方法需要LaireEngine初始化中的值。 如何使RespondEngine的初始化自动获取这些值,而不是必须在类LaireEngine中编写方法来返回值,然后将这些值作为参数传递给RespondEngine 我正在尝试为电报机器人构建一个框架 index.php require_once('init_lairfw.php'); //Load up LairEngin

我不知道该怎么办

我有两门课,LairEngine和RespondEngine

RespondEngine中包含某些方法,这些方法需要LaireEngine初始化中的值。 如何使RespondEngine的初始化自动获取这些值,而不是必须在类LaireEngine中编写方法来返回值,然后将这些值作为参数传递给RespondEngine

我正在尝试为电报机器人构建一个框架

index.php

require_once('init_lairfw.php');

//Load up LairEngine and RespondEngine 
$engine = new LairEngine;
$respond = new RespondEngine;

$respond->sendText("HEY");
spl_autoload_register(null, false);

spl_autoload_extensions('.mod.php, .lair.php');

function ModuleLoader($class) {
    if(file_exists(strtolower('modules/'.$class.'.mod.php')))   {
        include(strtolower('modules/'.$class.'.mod.php'));
    } 
}

function EngineLoader($class) {
    if(file_exists(strtolower('engine/'.$class.'.lair.php')))   {
        include(strtolower('engine/'.$class.'.lair.php'));
    }
}

spl_autoload_register('ModuleLoader');
spl_autoload_register('EngineLoader');
class LairEngine {
    private $config;
    private $url;
    private $data;
    private $datatypes;
    //The construct method is executed, and requrired information(incoming data, outgoing data) 
    //is processed and allocated for further use
    function __construct()
    {
        $this->config = json_decode(file_get_contents('config/.Engine'), true);
        $this->url = array(
            "https://api.telegram.org/bot".$this->config['token'],
            "https://api.telegram.org/bot".$this->config['token']."/getFile?file_id=",
            "https://api.telegram.org/file/bot".$this->config['token']."/"
        );

        $this->data = file_get_contents("php://input");
        $this->data = json_decode($this->data, TRUE);
        $this->process();
    }

    //Process the message and find out what kind of message it is
    private function process()
    {
        $k = array_keys($this->data['message']);
        switch($k[4])
        {
            case('reply_to_message'):
                $f=5; $this->dataTypes = array('msg_type' => 'reply');
                $this->dataTypes['reply_to_content'] = array_keys($this->data['message']['reply_to_message'][4]);
                break;

            case('new_chat_title'):
                $f=4; $this->dataTypes = array('msg_type' => 'group_name_change');
                break;

            case('new_chat_photo'):
                $f=4; $this->dataTypes = array('msg_type' => 'group_pic_change');
                break;

            case('forward_from'):
                $f=6; $this->dataTypes = array('msg_type' => 'forward');
                break;

            case('new_chat_participant'):
                $f=null; $this->dataTypes = array('msg_type' => 'new_user');
                break;

            case('left_chat_participant'):
                $f=null; $this->dataTypes = array('msg_type' => 'user_leave');
                break;

            default:
                $f=4; $this->dataTypes = array('msg_type' => 'original');

        }
        $this->dataTypes['msg_content'] = ($f!==null) ?  $k[$f] : null;
    }


    public function returnData()
    {
        $this->data['lair_return'] = $this->dataTypes;
        return $this->data;
    }

    public function returnDataTypes()
    {
        return $this->datatypes;
    }
    public function returnURL()
    {
        return $this->url;
    }

    public function returnchatdata()
    {
        return self::$data['message']['chat'];
    }

    public function throwError($e,$t)
    {
        switch($t)
        {
            case(0): trigger_error($e, E_USER_NOTICE); break;
            case(1): trigger_error($e, E_USER_WARNING); break;
            case(2): trigger_error($e, E_USER_ERROR); break;
        }
    }
}
class RespondEngine {
    public function sendText($response,$chatID=null,$url=null)
    {
        if(!$chatID)
        {
            //need the value from initalization of LairEngine
        }
        if(!$url)
        {
            //need the value from initalization of LairEngine

        }

        $request = $url."sendMessage?chat_id=".$chatID."&text=".urlencode($response);
        print_r($request);
        if(file_get_contents($request)) { return true; } else { return false; }
    }
}
init_lairfw.php

require_once('init_lairfw.php');

//Load up LairEngine and RespondEngine 
$engine = new LairEngine;
$respond = new RespondEngine;

$respond->sendText("HEY");
spl_autoload_register(null, false);

spl_autoload_extensions('.mod.php, .lair.php');

function ModuleLoader($class) {
    if(file_exists(strtolower('modules/'.$class.'.mod.php')))   {
        include(strtolower('modules/'.$class.'.mod.php'));
    } 
}

function EngineLoader($class) {
    if(file_exists(strtolower('engine/'.$class.'.lair.php')))   {
        include(strtolower('engine/'.$class.'.lair.php'));
    }
}

spl_autoload_register('ModuleLoader');
spl_autoload_register('EngineLoader');
class LairEngine {
    private $config;
    private $url;
    private $data;
    private $datatypes;
    //The construct method is executed, and requrired information(incoming data, outgoing data) 
    //is processed and allocated for further use
    function __construct()
    {
        $this->config = json_decode(file_get_contents('config/.Engine'), true);
        $this->url = array(
            "https://api.telegram.org/bot".$this->config['token'],
            "https://api.telegram.org/bot".$this->config['token']."/getFile?file_id=",
            "https://api.telegram.org/file/bot".$this->config['token']."/"
        );

        $this->data = file_get_contents("php://input");
        $this->data = json_decode($this->data, TRUE);
        $this->process();
    }

    //Process the message and find out what kind of message it is
    private function process()
    {
        $k = array_keys($this->data['message']);
        switch($k[4])
        {
            case('reply_to_message'):
                $f=5; $this->dataTypes = array('msg_type' => 'reply');
                $this->dataTypes['reply_to_content'] = array_keys($this->data['message']['reply_to_message'][4]);
                break;

            case('new_chat_title'):
                $f=4; $this->dataTypes = array('msg_type' => 'group_name_change');
                break;

            case('new_chat_photo'):
                $f=4; $this->dataTypes = array('msg_type' => 'group_pic_change');
                break;

            case('forward_from'):
                $f=6; $this->dataTypes = array('msg_type' => 'forward');
                break;

            case('new_chat_participant'):
                $f=null; $this->dataTypes = array('msg_type' => 'new_user');
                break;

            case('left_chat_participant'):
                $f=null; $this->dataTypes = array('msg_type' => 'user_leave');
                break;

            default:
                $f=4; $this->dataTypes = array('msg_type' => 'original');

        }
        $this->dataTypes['msg_content'] = ($f!==null) ?  $k[$f] : null;
    }


    public function returnData()
    {
        $this->data['lair_return'] = $this->dataTypes;
        return $this->data;
    }

    public function returnDataTypes()
    {
        return $this->datatypes;
    }
    public function returnURL()
    {
        return $this->url;
    }

    public function returnchatdata()
    {
        return self::$data['message']['chat'];
    }

    public function throwError($e,$t)
    {
        switch($t)
        {
            case(0): trigger_error($e, E_USER_NOTICE); break;
            case(1): trigger_error($e, E_USER_WARNING); break;
            case(2): trigger_error($e, E_USER_ERROR); break;
        }
    }
}
class RespondEngine {
    public function sendText($response,$chatID=null,$url=null)
    {
        if(!$chatID)
        {
            //need the value from initalization of LairEngine
        }
        if(!$url)
        {
            //need the value from initalization of LairEngine

        }

        $request = $url."sendMessage?chat_id=".$chatID."&text=".urlencode($response);
        print_r($request);
        if(file_get_contents($request)) { return true; } else { return false; }
    }
}
engine/lairengine.php

require_once('init_lairfw.php');

//Load up LairEngine and RespondEngine 
$engine = new LairEngine;
$respond = new RespondEngine;

$respond->sendText("HEY");
spl_autoload_register(null, false);

spl_autoload_extensions('.mod.php, .lair.php');

function ModuleLoader($class) {
    if(file_exists(strtolower('modules/'.$class.'.mod.php')))   {
        include(strtolower('modules/'.$class.'.mod.php'));
    } 
}

function EngineLoader($class) {
    if(file_exists(strtolower('engine/'.$class.'.lair.php')))   {
        include(strtolower('engine/'.$class.'.lair.php'));
    }
}

spl_autoload_register('ModuleLoader');
spl_autoload_register('EngineLoader');
class LairEngine {
    private $config;
    private $url;
    private $data;
    private $datatypes;
    //The construct method is executed, and requrired information(incoming data, outgoing data) 
    //is processed and allocated for further use
    function __construct()
    {
        $this->config = json_decode(file_get_contents('config/.Engine'), true);
        $this->url = array(
            "https://api.telegram.org/bot".$this->config['token'],
            "https://api.telegram.org/bot".$this->config['token']."/getFile?file_id=",
            "https://api.telegram.org/file/bot".$this->config['token']."/"
        );

        $this->data = file_get_contents("php://input");
        $this->data = json_decode($this->data, TRUE);
        $this->process();
    }

    //Process the message and find out what kind of message it is
    private function process()
    {
        $k = array_keys($this->data['message']);
        switch($k[4])
        {
            case('reply_to_message'):
                $f=5; $this->dataTypes = array('msg_type' => 'reply');
                $this->dataTypes['reply_to_content'] = array_keys($this->data['message']['reply_to_message'][4]);
                break;

            case('new_chat_title'):
                $f=4; $this->dataTypes = array('msg_type' => 'group_name_change');
                break;

            case('new_chat_photo'):
                $f=4; $this->dataTypes = array('msg_type' => 'group_pic_change');
                break;

            case('forward_from'):
                $f=6; $this->dataTypes = array('msg_type' => 'forward');
                break;

            case('new_chat_participant'):
                $f=null; $this->dataTypes = array('msg_type' => 'new_user');
                break;

            case('left_chat_participant'):
                $f=null; $this->dataTypes = array('msg_type' => 'user_leave');
                break;

            default:
                $f=4; $this->dataTypes = array('msg_type' => 'original');

        }
        $this->dataTypes['msg_content'] = ($f!==null) ?  $k[$f] : null;
    }


    public function returnData()
    {
        $this->data['lair_return'] = $this->dataTypes;
        return $this->data;
    }

    public function returnDataTypes()
    {
        return $this->datatypes;
    }
    public function returnURL()
    {
        return $this->url;
    }

    public function returnchatdata()
    {
        return self::$data['message']['chat'];
    }

    public function throwError($e,$t)
    {
        switch($t)
        {
            case(0): trigger_error($e, E_USER_NOTICE); break;
            case(1): trigger_error($e, E_USER_WARNING); break;
            case(2): trigger_error($e, E_USER_ERROR); break;
        }
    }
}
class RespondEngine {
    public function sendText($response,$chatID=null,$url=null)
    {
        if(!$chatID)
        {
            //need the value from initalization of LairEngine
        }
        if(!$url)
        {
            //need the value from initalization of LairEngine

        }

        $request = $url."sendMessage?chat_id=".$chatID."&text=".urlencode($response);
        print_r($request);
        if(file_get_contents($request)) { return true; } else { return false; }
    }
}
engine/respondengine.php

require_once('init_lairfw.php');

//Load up LairEngine and RespondEngine 
$engine = new LairEngine;
$respond = new RespondEngine;

$respond->sendText("HEY");
spl_autoload_register(null, false);

spl_autoload_extensions('.mod.php, .lair.php');

function ModuleLoader($class) {
    if(file_exists(strtolower('modules/'.$class.'.mod.php')))   {
        include(strtolower('modules/'.$class.'.mod.php'));
    } 
}

function EngineLoader($class) {
    if(file_exists(strtolower('engine/'.$class.'.lair.php')))   {
        include(strtolower('engine/'.$class.'.lair.php'));
    }
}

spl_autoload_register('ModuleLoader');
spl_autoload_register('EngineLoader');
class LairEngine {
    private $config;
    private $url;
    private $data;
    private $datatypes;
    //The construct method is executed, and requrired information(incoming data, outgoing data) 
    //is processed and allocated for further use
    function __construct()
    {
        $this->config = json_decode(file_get_contents('config/.Engine'), true);
        $this->url = array(
            "https://api.telegram.org/bot".$this->config['token'],
            "https://api.telegram.org/bot".$this->config['token']."/getFile?file_id=",
            "https://api.telegram.org/file/bot".$this->config['token']."/"
        );

        $this->data = file_get_contents("php://input");
        $this->data = json_decode($this->data, TRUE);
        $this->process();
    }

    //Process the message and find out what kind of message it is
    private function process()
    {
        $k = array_keys($this->data['message']);
        switch($k[4])
        {
            case('reply_to_message'):
                $f=5; $this->dataTypes = array('msg_type' => 'reply');
                $this->dataTypes['reply_to_content'] = array_keys($this->data['message']['reply_to_message'][4]);
                break;

            case('new_chat_title'):
                $f=4; $this->dataTypes = array('msg_type' => 'group_name_change');
                break;

            case('new_chat_photo'):
                $f=4; $this->dataTypes = array('msg_type' => 'group_pic_change');
                break;

            case('forward_from'):
                $f=6; $this->dataTypes = array('msg_type' => 'forward');
                break;

            case('new_chat_participant'):
                $f=null; $this->dataTypes = array('msg_type' => 'new_user');
                break;

            case('left_chat_participant'):
                $f=null; $this->dataTypes = array('msg_type' => 'user_leave');
                break;

            default:
                $f=4; $this->dataTypes = array('msg_type' => 'original');

        }
        $this->dataTypes['msg_content'] = ($f!==null) ?  $k[$f] : null;
    }


    public function returnData()
    {
        $this->data['lair_return'] = $this->dataTypes;
        return $this->data;
    }

    public function returnDataTypes()
    {
        return $this->datatypes;
    }
    public function returnURL()
    {
        return $this->url;
    }

    public function returnchatdata()
    {
        return self::$data['message']['chat'];
    }

    public function throwError($e,$t)
    {
        switch($t)
        {
            case(0): trigger_error($e, E_USER_NOTICE); break;
            case(1): trigger_error($e, E_USER_WARNING); break;
            case(2): trigger_error($e, E_USER_ERROR); break;
        }
    }
}
class RespondEngine {
    public function sendText($response,$chatID=null,$url=null)
    {
        if(!$chatID)
        {
            //need the value from initalization of LairEngine
        }
        if(!$url)
        {
            //need the value from initalization of LairEngine

        }

        $request = $url."sendMessage?chat_id=".$chatID."&text=".urlencode($response);
        print_r($request);
        if(file_get_contents($request)) { return true; } else { return false; }
    }
}

您不能通过对象边界访问未公开的值,或者通过声明为公共的或通过“getter”方法发布的值。这就是对象定向的要点。如果你真的需要,你的架构很可能是错误的。好的,你能告诉我如何重新设计我的架构吗。它仍然是它的基本形式,所以如果你能给我一些提示我会很感激的。当然我们都会尝试,但是你必须向我们展示你的架构,并解释你正在尝试做什么。我们可能很好,但我们无法神奇地猜测所有这些:-)@arkascha我已经用我的代码链接更新了这个问题,如果你能快速查看一下,我将不胜感激:)谢谢我已经将代码放回原处,你使用了错误的格式代码。