stef
stef

Reputation: 27749

Cake PHP class variables

I have a class in CakePHP with several methods that contain these two lines:

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

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

I'm not sure if it should go in a __construct or ... ?

Upvotes: 1

Views: 1446

Answers (1)

pleasedontbelong
pleasedontbelong

Reputation: 20102

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

Upvotes: 4

Related Questions