我有以下课程:
class countries {
protected $_table = 'countries';
public function __construct() {
$this->mysqli = new mysqli(db_host, db_user, db_pass, db_name);
}
public function dropdown_menu() {
$query = "SELECT * FROM $this->_table ORDER BY name ASC";
$result = $this->mysqli->query($query) or die ($this->mysqli->error);
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
$results[] = array('iso' => $row['iso'], 'name' => $row['name'], 'flag' => $row['flag']);
}
}
}
当我在课外使用时:
$countries = new countries();
$countries->dropdown_menu();
$results = $countries;
foreach($results as $xresult)
{
$template->newBlock('dropdown_menu');
$template->assign('iso', $xresult['iso']);
}
这行不通。当我使用
foreach($results as $xresult)
{
$template->newBlock('dropdown_menu');
$template->assign('iso', $xresult['iso']);
}
在 dropdown_menu 函数的类内部,它可以工作。
类外的 print_r ($results) 给出以下结果:
countries Object ( [_table:protected] => countries [mysqli] => mysqli Object ( ) )
我如何在课堂外使用 $results?