douya7282 2018-04-23 13:06
浏览 52

将对象存储到php中的数组或arraylist

I'm coming from Java and just continued learning PHP. So, I'm trying to follow some similar patterns in that I use in Java to get values from database.

There's just one problem that I don't know how to solve. I can't find a way to properly store objects (which has its values / attributes set using the set() methods) to an Array or ArrayList. I think that PHP don't have ArrayList or List or HashMaps

But anyways, I basically want to know the correct idiom,syntax or way to store objects which I instantiated by keyword new to the common type of PHP collection where I can easily retrieve objects from later by using get() methods.

    function getAllAnnouncementByMostRecent()
    {
        try{
            $SQL = "CALL getAllAnnouncementByMostRecent()";
            $spGetAnnouncements = $this->connection->prepare($SQL);
            $spGetAnnouncements->execute();
            $resultSet = $spGetAnnouncements->fetchAll(PDO::FETCH_ASSOC);

            $announcementList = [];
            foreach($resultSet as $row){
                $announcement = new Announcement();
                $announcement->setId($row['announcement_id']);
                $announcement->setTitle($row['announcement_title']);
                $announcement->setContent($row['announcement_content']);
                $announcement->setDateAdded($row['date_added']);
                $announcementList[] = $announcement;
            }
            return $announcementList;
        }catch (PDOException $e){
            die($e->getMessage());
        }
    }

So I tried to retrieve data from it but got an error stating that there's

Warning: Invalid argument supplied for foreach()

<div class="bottom-menu-content-container">
    <?php
        $announcementDaoImpl = new AnnouncementDaoImpl($pdo);
        $announcementList = $announcementDaoImpl->getAllAnnouncementByMostRecent();
        foreach($announcementList as $key => $value)
        {
            echo "<label>". $value. "</label>";
        }
    ?>
</div>

I tried to var_dump() the contents of $announcementList and I get the ff:

array(1) { [0]=> object(Announcement)#4 (4) { ["id":"Announcement":private]=> int(1) ["title":"Announcement":private]=> string(23) "Test Announcement Title" ["content":"Announcement":private]=> string(25) "Test Announcement Content" ["dateAdded":"Announcement":private]=> string(19) "2018-04-22 16:12:06" } } 

I'd appreciate any help.

Thank you.

--- EDIT (SOLVED) ---

Thanks everyone for your suggestions and help. It was solved by simply accessing the get() method of the object as suggested by William Perron and ADyson

<div class="bottom-menu-content-container">
    <?php
        $announcementDaoImpl = new AnnouncementDaoImpl($pdo);
        $announcementList = $announcementDaoImpl->getAllAnnouncementByMostRecent();
        foreach($announcementList as $key => $value)
        {
            echo $value->getTitle(); //no need to cast
        }
        ?>
</div>

To those who might see this question during search who might also be coming from a different programming language, the only thing that I was confused was the casting of objects.

Thanks.

--- end of edit ----

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 MATLAB动图问题
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名