while ループから別のクラスの別のメソッドに変数を渡そうとしています。私はOOPを初めて使用するので、これを行う方法がわからないので、しばらくの間グーグルで検索していましたが、うまくいきませんでした。私のコードは以下の通りです:
class ImageRenderer extends CommentReplyRenderer {
function __construct() {
$comments = new ImageIterator();
$test = $comments->getAll();
while($id = $test->fetch(PDO::FETCH_ASSOC)){
echo $id['photo_id'];
}
}
}
上記のクラスには$id['photo_id']
、以下のメソッドに渡す必要がある変数があります。
class CommentIterator extends CommentReplyIterator
{
public $conn;
public $idOfComment;
public $stmt;
public function getAll($idOfComment)
{
$this->stmt = $this->conn->prepare('SELECT * FROM `comments` WHERE `photo_id` = :picture_id');
$this->stmt->bindValue(':picture_id', $idOfComment);
$this->stmt->execute();
return $this->stmt;
}
}
$idOfComment
変数は、メソッドに指定された引数内に配置する必要がありますgetAll()
。私はそれを渡す方法を理解するまで、今のところこのように呼んでいます:
$comment1 = new CommentIterator();
$testing = $comment1->getAll($id['photo_id']);
while($boom = $testing->fetch(PDO::FETCH_ASSOC)){
echo $boom['comment'];
}
行内に配置しようとしていることがわかりますが$testing = $comment->getAll($id['photo_id']);
、未定義の変数エラーが発生します。
拡張しているクラスにはCommentReplyRenderer
何も含まれていません
class CommentReplyIterator
{
public $conn;
public function __construct()
{
try{
$this->conn = new PDO('mysql:host=localhost;dbname=testing', 'test','BOOMSHAKALAKA');
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
echo'ERROR: ' . $e->getMessage();
}
}
}