将 PHP 对象传递给 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6351949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:23:06  来源:igfitidea点击:

passing PHP objects to javascript

phpjavascriptobject

提问by William Sham

I have objects in php that each represents a "item" and all info associated with it.

我在 php 中有对象,每个对象都代表一个“项目”以及与之相关的所有信息。

And when the user browses the page, these objects should be passed to javascript. Ideally, mirroring the same structure, so I can use Raphael to display each item and its info as separate shapes on my site.

当用户浏览页面时,这些对象应该传递给 javascript。理想情况下,镜像相同的结构,因此我可以使用 Raphael 在我的网站上将每个项目及其信息显示为单独的形状。

However, how do you get an object from php to javascript?

但是,如何将对象从 php 获取到 javascript?

回答by

You can convert the PHP object to an array, and then use JSON function to encode it. After that, decode it from JavaScript. Here are some basic steps:

您可以将 PHP 对象转换为数组,然后使用 JSON 函数对其进行编码。之后,从 JavaScript 解码它。以下是一些基本步骤:

  1. Convertthe PHP object to an array.

  2. Using json_encode()to encode that PHP array.

  3. PassJSON-encoded data to PHP

  4. Decode JSON from JavaScript using JSON.parseor you can use jQuery.parseJSONto do that.

  1. PHP 对象转换为数组。

  2. 使用json_encode()以编码PHP阵列。

  3. JSON 编码的数据传递给 PHP

  4. 使用 JavaScript 从 JavaScript 解码 JSON,JSON.parse或者您可以使用它jQuery.parseJSON来执行此操作。

This is an interesting tutorial about passing a JavaScript object to a PHP object. You might find it useful by watching some other featured/related videos.

这是一个关于将JavaScript 对象传递给 PHP 对象的有趣教程。通过观看其他一些精选/相关视频,您可能会发现它很有用。

Hope this helps.

希望这可以帮助。

回答by Chris Baker

Late answer, but I am very surprised no one mentioned what is, arguably, the "correct" way to deploy this functionality.

迟到的答案,但我很惊讶没有人提到部署此功能的“正确”方法是什么。

Implement the JsonSerializableinterface on your object. This interface defines one abstract method, jsonSerialize. That method should return the array representation of your object.

在您的对象上实现JsonSerializable接口。该接口定义了一个抽象方法jsonSerialize. 该方法应该返回对象的数组表示。

The jsonSerializewill be called when you attempt to use json_encodeon the object. You can think of this interface as a sort of "magic method" specific to the native JSON encode function, equivalent to __toStringfor strings.

jsonSerialize当您尝试使用将被调用json_encode的对象。您可以将此接口视为一种特定于原生 JSON 编码函数的“魔术方法”,相当于__toString字符串。

Putting it in action, you have an object that looks like this:

把它付诸实践,你有一个看起来像这样的对象:

class MyFoo implements JsonSerializable {
    public $bar = 'hello';
    public $baz = 'world';

    public function jsonSerialize () {
        return array(
            'bar'=>$this->bar,
            'baz'=>$this->baz
        );
    }
}

$myFooInstance= new MyFoo();
echo json_encode($myFooInstance); // {"bar":"hello","baz":"world"}

When I am implementing this in my projects, I usually put a toArraymethod in the objects, which generates the array, and I have jsonSerializeuse it:

当我在我的项目中实现这个时,我通常toArray在对象中放置一个方法,它生成数组,我已经jsonSerialize使用了它:

public function jsonSerialize () { return $this->toArray(); }

public function jsonSerialize () { return $this->toArray(); }

...this way I can also make use of the array form, if I so choose. This is also handy if you'll be implementing object serialization with __sleep

...这样我也可以使用数组形式,如果我愿意的话。如果您要实现对象序列化,这也很方便__sleep

Documentation

文档

回答by dlopezgonzalez

I use this:

我用这个:

<script>
var js_data = '<?php echo json_encode($php_data); ?>';
var js_obj_data = JSON.parse(js_data );
</script>

$php_data can be since simple arrays to arrays of objects.

$php_data 可以是简单的数组到对象数组。

回答by datasage

You will be using JSON to encode the PHP objects so that they can be accessed by Javascript. The specifics on how to do that depends on your application.

您将使用 JSON 对 PHP 对象进行编码,以便 Javascript 可以访问它们。如何做到这一点的细节取决于您的应用程序。

PHP has the functions json_encode and json_decode for this purpose.

为此,PHP 具有 json_encode 和 json_decode 函数。