問題点
・メソッドが未定義になっているエラーが発生しているので解決したい。
試したこと
・試したこと、メソッド定義している部分の見直し。
エラー内容
PHP Fatal error: Uncaught Error: Call to undefined method Player::findAll() in /Applications/MAMP/htdocs/01_php_mvc lesson/Controllers/PlayerController.php:18
php
1//メソッド部分// 2class Player extends Db { 3 private $table = 'players'; 4 5 public function __construct($dbh = null) { 6 parent::__construct($dbh); 7 } 8public function findById($id = 0):Array { 9 $sql = 'SELECT * FROM '.$this->table; 10 $sql .= ' WHERE id = :id'; 11 $sth = $this->dbh->prepare($sql); 12 $sth->bindParam(':id', $id, PDO::PARAM_INT); 13 $sth->execute(); 14 $result = $sth->fetch(PDO::FETCH_ASSOC); 15 return $result; 16 } 17} 18 19class PlayerController { 20 private $request;//リクエストパラメータ(GET,POST) 21 private $Player;//Playerモデル 22 23 public function __construct() { 24 //リクエストパラメータの取得 25 $this->request['get'] = $_GET; 26 $this->request['post'] = $_POST; 27 28 public function index() { 29 $players = $this->Player->findAll(); //←エラー箇所 30 $params = [ 31 'players' => $players 32 ]; 33 return $params; 34 } 35 36//メソッド呼び出し 37 public function view() { 38 if(empty($this->request['get']['id'])) { 39 echo '指定のパラメータが不正です。このページを表示できません。'; 40 exit; 41 } 42 $player = $this->Player->findById($this->request['get']['id']); 43 $parmas = [ 44 'player' => $player 45 ]; 46 return $parmas; 47 } 48 }
回答2件
あなたの回答
tips
プレビュー