私は自分の注文MVを述べました。単純なモデル+コントローラ・クラスを持っていて、別の関数で呼び出されたときに認識できるようにモデルでvarを取得できません。
コントローラクラスが
class StaffController {
protected $module = "staff";
public function __construct()
{
//include the staff Model
require_once(MODEL_PATH.$this->module.'.php');
}
public function index() {
// we store all the posts in a variable
$staff = Staff::all();
header('Content-Type: application/json');
echo json_encode($staff);
}
そして$staff = Staff::all();
モデルクラスを呼び出し、認識されないその$list
変数です:
class Staff {
public $list = array();
public function __construct() {
$this->list = [];
}
public static function all() {
//get all the staff
$temp = [];
$db = Db::getInstance();
$req = $db->query('SELECT * FROM data ORDER BY ParentID ASC');
// we create a list of Post objects from the database results
foreach($req->fetchAll() as $staff) { array_push($temp,$staff);
self::reorderOrg($temp);
return $final;
}
private static function reorderOrg($array, $parent = 0, $depth = 0){
//reorganise org
for($i=0, $ni=count($array); $i < $ni; $i++){
//check if parent ID same as ID
if($array[$i]['parentID'] == $parent){
array_push($this->list ,$array[$i]); //***** no being recognized
self::reorderOrg($array, $array[$i]['id'], $depth+1);
}
}
return true;
}
}
次のエラーが発生しましたオブジェクトコンテキストに存在しない場合は$ thisを使用し、モデルクラスのarray_pushとの間には$this->list
がありません。あなたの関数が静的あるので、それはそれ自身のクラスのつく内で使用することができるので、セットにプライベートVARを行う方法
をあなたは静的メソッドで、「この」コンテキストを持っていません。 '$ this-> list'の代わりに' self :: $ list'を呼び出さなければなりません。 – rinukkusu