在类方法之间传递变量

时间:2013-11-04 03:09:32

标签: php oop

我试图将变量从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();
        }
    }

}

0 个答案:

没有答案
相关问题