加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

将PHP Class对象传递给Javascript并在Javascript函数中访问其属

发布时间:2020-12-13 21:33:13 所属栏目:PHP教程 来源:网络整理
导读:我在访问我创建的 Javascript函数中的对象属性时遇到问题.该对象最初是一个PHP对象,我使用json_encode()编码为JSON 我有1个PHP文件和1个外部JS文件. 在我的PHP文件中,我有ff: ?php $announcementDaoImpl = new AnnouncementDaoImpl($pdo); $announcementLis
我在访问我创建的 Javascript函数中的对象属性时遇到问题.该对象最初是一个PHP对象,我使用json_encode()编码为JSON

我有1个PHP文件和1个外部JS文件.

在我的PHP文件中,我有ff:

<?php
    $announcementDaoImpl = new AnnouncementDaoImpl($pdo);
    $announcementList = $announcementDaoImpl->getAllAnnouncementByMostRecent();
    foreach($announcementList as $key => $value): ?>
        <tr>
        <td><?php echo $value->getId(); ?></td>
        <td><?php echo $value->getTitle(); ?></td>
        <td><?php echo $value->getDateAdded(); ?></td>
        <?php echo json_encode($value); ?> 
        <td>
        <a href="#" onclick="showEditModal('modalBox',<?php echo json_encode($value); ?>)">Edit</a>
        </td>
        </tr>
    <?php endforeach; ?>

我可以通过使用< td>上的get()方法来获取和显示值.但是,当我通过json_encode($value)将其编码为JSON时,我什么都没得到.

线

<?php echo json_encode($value); ?>

回声{} {} {} {} {} {} {} {} {} {} {} {} {}这似乎是空对象.

我的目标是能够将$value php对象传递给javascript方法showEditModal(),然后访问方法块中的对象属性.像object.propertyname这样的东西

下面是javascript方法showEditModal()实现.

function showEditModal(modalDivId,object){
    alert(object.title); //returns undefined

    var modalBox = document.getElementById(modalDivId);
    var modalContentValues = document.getElementById("modalContentValues");
    modalBox.style.display = "block";
    var node = document.createElement("p");
    var text = document.createTextNode(object); //shows [object Object]
    node.style.display = "inline";
    node.appendChild(text);
    modalContentValues.appendChild(node);
}

—编辑—

这是类定义.

class Announcement {
    private $id;
    private $title;
    private $content;
    private $dateAdded;


    public function getContent()
    {
        return $this->content;
    }

    public function setContent($content)
    {
        $this->content = $content;
    }

    public function getDateAdded()
    {
        return $this->dateAdded;
    }

    public function setDateAdded($dateAdded)
    {
        $this->dateAdded = $dateAdded;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }
}

– 编辑结束 –

这是我的浏览器上显示的内容.

enter image description here

我已经在javascript方法中添加了一些注释以获取更多详细信息.

我很感激任何帮助.

谢谢.

解决方法

您可以实现 JsonSerializable接口来定义在序列化对象时要显示的属性;例如:

<?php 

class User implements JsonSerializable
{
    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function jsonSerialize()
    {
        return [
            'name' => $this->name,];
    }
}

echo json_encode(new User('Bob')); // {"name":"Bob"}

这是以透明和隐式方式处理PHP中的对象序列化的惯用方法,它允许您为对象的json表示定义首选形状/格式,并将其封装在其类定义中.

在序列化嵌套对象结构时非常方便,例如,当您使用值对象时 – 这是一个(人为的)示例:

class Latitude implements JsonSerializable
{
    private $degree;

    public function __construct(float $degree)
    {
        // validation etc.
        $this->degree = $degree;
    }

    public function jsonSerialize(): float
    {
        return $this->degree;
    }
}

class Longitude implements JsonSerializable
{
    private $degree;

    public function __construct(float $degree)
    {
        // validation etc.
        $this->degree = $degree;
    }

    public function jsonSerialize(): float
    {
        return $this->degree;
    }
}

class Coordinate implements JsonSerializable
{
    public function __construct(Latitude $latitude,Longitude $longitude)
    {
        $this->latitude = $latitude;
        $this->longitude = $longitude;
    }

    public function jsonSerialize(): array
    {
        return [
          'latlng' => [$this->latitude,$this->longitude],];
    }
}

$coordinate = new Coordinate(new Latitude(0),new Longitude(0));

echo json_encode($coordinate); // {"latlng":[0,0]}

示例:https://repl.it/repls/RelievedLightcyanFolders

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读