Answer the question
In order to leave comments, you need to log in
Why can't you pass a property of the current object to a closure?
Greetings colleagues. Help to understand the intricacies of the language. The second hour I can not find anything even close similar. The manual from SO has already subtracted everything)) The error is as follows:
PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ')', Apparently in this place $this -> db
Code:
class Orders {
function __construct(Database $db) {
$this->db = $db;
}
function createOrder(array $data) {
$this->db->transaction(function($this->db) { // ошибка в этой строке
$id1 = $this->db->insert('...');
$id2 = $this->db->insert('...');
$res = $this->db->update('...');
});
}
}
$orders = new Orders($db);
function createOrder(array $data) {
$db = $this->db;
$this->db->transaction(function($db) { // Так работает
$this->db->transaction(function() use ($db) { // И так тоже
$id1 = $this->db->insert('...');
$id2 = $this->db->insert('...');
$res = $this->db->update('...');
});
}
Answer the question
In order to leave comments, you need to log in
Since PHP 5.4 you have $this available inside an anonymous function declared inside a class.
you don't have to send anything there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question