简体   繁体   English

Cake PHP类变量

[英]Cake PHP class variables

I have a class in CakePHP with several methods that contain these two lines: 我在CakePHP中有一个类,有几个方法包含这两行:

App::import('Core', 'HttpSocket');
$HttpSocket = new HttpSocket();

Which is then called 然后调用哪个

$result = $HttpSocket->post('http://domain.com', $dataArr);

How can I put the first two lines in to a class variable $socket which will then allow me to do 如何将前两行放入类变量$socket ,然后允许我这样做

$result = $socket->post("http://domain.com", $dataArr);

I'm not sure if it should go in a __construct or ... ? 我不确定它是否应该进入__construct或......?

it should be something like this: 它应该是这样的:

class MyClass {

    private $socket;

    public function __construct(){
        App::import('Core', 'HttpSocket');
        $this->socket = new HttpSocket();
    }

    public function my_function($data) {
        $result = $this->socket->post("http://domain.com", $data);
    }
}

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM