Как установить параметр функции по умолчанию внутри класса?

Как установить параметр функции по умолчанию внутри класса?

class tag_0_model {
protected $response_message;

public function __construct() {
$this->response_message = array(
"debug_message" => $debug_message =array(),
"error_message" => $error_message =array(),
"success_message" => $success_message =array(),
"warning_message" => $warning_message =array()
);
}

// sure this is parse error , not work
public function insert_ignore($response_message = $this->response_message, $data) {
//
}

Я стараюсь использовать как

class tag_0_controller {
protected $response_message;

public function __construct() {
$this->response_message = array(
"debug_message" => $debug_message =array(),
"error_message" => $error_message =array(),
"success_message" => $success_message =array(),
"warning_message" => $warning_message =array()
);
}

public function method() {$data = ...;
$this->tag_0_model->insert_ignore($data);// or$response_message = $this->response_message;
$data = ...;
$this->tag_0_model->insert_ignore($response_message, $data);
}
}

0

Решение

1 / Вы не можете поместить обязательные параметры после опциональных параметров, в определении функции «insert_ignore» должно быть значение по умолчанию для «$ data»

2 / Вы не можете поместить свое свойство в качестве параметра по умолчанию vlaue. Параметры по умолчанию должны существовать во время компиляции, поэтому это должна быть константа или константа класса, я предлагаю вам установить значение по умолчанию в «null» и заменить его внутри своей функции:

public function insert_ignore($data = array(), $response_message = null)
{
if( $response_message === null ) {
$response_message = $this->response_message;
}
// ...
}

// Call this function by
$this->tag_0_model->insert_ignore($data);

РЕДАКТИРОВАТЬ Обновленный код

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector