OOP的新手,我想通过ajax从PHP发回数据来练习一下。我在这做错了什么?如果我将代码更改为程序,它可以工作。这是OOP:
if (isset($_POST['fruity'])) {
$start_fruity = new Fruity_draft();
$start_fruity->send_json();
}
class Fruity_draft {
public $banned = $_POST['banned'];
public $players = $_POST['players'];
public $random_civs = $_POST['random_civs'];
public $array_list = [];
public $send_json['banned'] = $banned;
function __construct($send_json) {
$this->send_json = $send_json;
}
function send_json() {
echo json_encode($this->send_json);
}
}
答案 0 :(得分:2)
首先,您忘记了将参数传递给构造函数,它需要一个数组。
function __construct($send_json) {
在通话中,您不发送任何内容
$start_fruity = new Fruity_draft();
这会发出警告Warning: Missing Argument 1
和通知,Notice: Undefined variable: send_json
其次,您应该在构造函数中移动类变量的初始化。
class Fruity_draft {
public $banned;
public $players;
public $random_civs;
public $array_list;
public $send_json;
function __construct($send_json) {
$this->banned = 'banned';
$this->players = 'players';
$this->random_civs = 'random_civs';
$this->send_json = $send_json;
$this->send_json['banned'] = $this->banned;
}
...
}
答案 1 :(得分:1)
那不是真正的OOP :)。你应该从课堂上回复一些东西,而不是回声。 此外,您应该将数据从其他函数发送到类...在构造函数中或使用方法set_post_data()或其他东西......
简单:
{{1}}