所以我一直都在努力學習如何創建一個MVC,但我不能弄清楚怎麼回事錯試圖讓非對象錯誤的財產在我的PHP代碼
好像我忘了張貼錯誤: 公告:嘗試獲取第2行的C:\ xampp \ htdocs \ my_mvc \ views \ producten \ show.php中非對象的屬性
注意:試圖獲取C:\ xampp \ htdocs中非對象的屬性第3行上的\ my_mvc \ views \ producten \ show.php
注意:試圖獲取第4行的C:\ xampp \ htdocs \ my_mvc \ views \ producten \ show.php中的非對象的屬性
個觀點: 的index.php:
<p>Here is a list of all producten:</p>
<?php foreach($producten as $product) { ?>
<p>
<?php echo $product->etal; ?>
<a href='?controller=producten&action=show&etal=<?php echo $product->etal; ?>'>See content</a>
</p>
<?php } ?>
show.php:
<p> requested products</p>
<p><?php echo $product->etal; ?></p>
<p><?php echo $product->naam; ?></p>
<p><?php echo $product->prijs; ?></p>
控制器: producten_controller.php:
<?php
class productenController {
public function index() {
$product = Product::all();
require_once('views/producten/show.php');
}
public function show() {
if (!isset($_GET['etal']))
return call('pages', 'error');
$product = Product::find($_GET['etal']);
require_once('views/producten/show.php');
}
}
?>
模型: product.php:
<?php
class Product {
public $etal;
public $naam;
public $prijs;
public $omsch;
public function __construct($etal, $naam, $prijs,$omsch) {
$this->etal = $etal;
$this->naam = $naam;
$this->prijs = $prijs;
$this->omsch = $omsch;
}
public static function all() {
$list = [];
$db = Db::getInstance();
$req = $db->query('SELECT * FROM producten');
foreach($req->fetchAll() as $product) {
$list[] = new product($product['etal'], $product['naam'], $product['prijs'],$product['omsch']);
}
return $list;
}
public static function find($etal) {
$db = Db::getInstance();
$etal = intval($etal);
$req = $db->prepare('SELECT * FROM producten WHERE etal = :etal');
$req->execute(array('etal' => $etal));
$product = $req->fetch();
return new product($product['etal'], $product['naam'], $product['prijs'],$product['omsch']);
}
}
?>
如果有人能幫助我,這將是驚人的
請發佈你想修復的錯誤日誌。 – thodic
似乎忘記了,現在添加它。 –