出現這個錯誤訊息的原因是在靜態方法中使用$this來呼叫本類別的方法,在php中,靜態方法呼叫使用self。代碼如下
public static function editPlaceSelect($places=[], $city_id=-1){ if(is_array($places) && count($places)>0){ foreach ($places as $place){ $this->areaPlace($city_id, $place['area_id'], $place['address']); } }else{ echo '<li>'; echo '<select name="row_area_id" class="w-200 row_area_id">'; echo '<option value="">-暂未开放-</option>'; echo '</select>'; echo '<input type="text" name="row_area_address" class="w-300">'; echo '</li>'; } } public static function areaPlace($city_id, $area_id, $address){ echo '<li>'; echo '<select name="row_area_id" class="w-200 row_area_id">'; $this->areaSelect($city_id, $area_id); echo '</select>'; echo '<input type="text" name="row_area_address" class="w-300" value="'.$address.'">'; echo '</li>'; }
紅色代碼處應改為:
self::areaPlace($city_id, $place['area_id'], $place['address']); $this->areaSelect($city_id, $area_id);
以上就介紹了Using $this when not in object context錯誤訊息,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。