关于 JavaScript、PHP 以及两者之间传递对象引用的问题
首先,为了说明我想要的行为,请查看以下 JS 代码:
let firstObj;
let secondObj;
firstObj = secondObj = {innerAttribute: "the initial value"};
firstObj.innerAttribute = "a new value";
console.log(secondObj.innerAttribute);
这将输出“一个新值”,因为 firstObj 和 secondaryObj 指向同一个对象。
在我的应用程序中,我对 PHP 脚本进行 ajax 调用,可能如下所示:
<?php
$phpArray = array();
$phpArray["key1"] = $phpArray["key2"] = array("firstValue","secondValue");
json_encode($phpArray);
?>
问题是,当在 JS 中接收到此数组时,key1 和 key2 并不指向同一个对象。如果我在 JS 中使用成功检索到的对象执行此操作:
retrievedObject["key1"][0] = "firstValueModified";
console.log(retrievedObject["key2"][0]);
它输出“firstValue”,表明 php 创建了两个不同的对象,而不是指向同一对象的两个指针。我尝试使用 &在 PHP 中是这样的:
<?php
$phpArray = array();
$phpArray["key1"] = array("firstValue","secondValue");
$phpArray["key2"] = &$phpArray["key1"];
json_encode($phpArray);
?>
但这也没有产生所需的行为。
这些都是我想要做的非常基本的示例,因为我的实际代码要复杂得多。 我只是想知道是否可以让 PHP 返回一个对象到 JS,该对象具有指向同一内部对象的多个属性或索引,这样对一个对象的更改就是对两者的更改。
实际原因我需要这个是因为我需要导入一个对象,其中每个项目都有一个数字引用和关联引用(就像 MYSQLI_BOTH 所做的那样,尽管它似乎是通过创建两个不同的对象来实现的),并且我需要能够修改中的每个项目JS 通过数字或关联引用并将其反映在另一个中。
我想做的事情可能吗?
To start with, to illustrate the behavior I desire, look at the following JS code:
let firstObj;
let secondObj;
firstObj = secondObj = {innerAttribute: "the initial value"};
firstObj.innerAttribute = "a new value";
console.log(secondObj.innerAttribute);
This would output "a new value" since firstObj and secondObj are pointing to the same object.
In my application, I am making an ajax call to a PHP script that might look something like this:
<?php
$phpArray = array();
$phpArray["key1"] = $phpArray["key2"] = array("firstValue","secondValue");
json_encode($phpArray);
?>
The problem is that when this array is received in the JS, key1 and key2 aren't pointing to the same object. If I do this in JS with the successfully retrieved object:
retrievedObject["key1"][0] = "firstValueModified";
console.log(retrievedObject["key2"][0]);
It outputs "firstValue", indicating that php created two different objects rather than two pointers to the same object. I tried using the & in PHP like this:
<?php
$phpArray = array();
$phpArray["key1"] = array("firstValue","secondValue");
$phpArray["key2"] = &$phpArray["key1"];
json_encode($phpArray);
?>
But this did not yield the desired behavior either.
These are all very basic examples of what I'm trying to do, as my actual code is much more complex. I just want to know if it's possible to get PHP to return an object to JS with multiple properties or indicies pointing to the same inner object such that a change to one is a change to both.
The practical reason I need this is because I need to import an object where each item has both a numerical and associative reference to it(like what MYSQLI_BOTH does, though it seems to do it by making two different objects), and I need to be able to modify each item in JS via either the numerical or associative reference and have it reflected in the other.
Is what I'm trying to do possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您根本不需要使用引用/指针。 指针不绑定到数据值,而是绑定到内存中的位置。它是一种节省内存或生成高效代码的机制。
更简单地说,变量就像可以放置东西的房子,而指针只是地址。
如果您的 PHP 服务器从 MySQL 数据库中提取数据,并且希望该数据完整地传输到客户端浏览器以便可以使用 JavaScript 进行处理,那么您必须将该数据放入对象并在从服务器发送之前将其序列化为 JSON。
该对象可以是 MySQLi 数据库扩展在您执行搜索时输出的
mysqli_result
对象,或者您可以创建自定义数据保存类:然后您可以使用 PHP 函数
serialize
code> 将该数据转换为 JSON 字符串。参考:https://www.php.net/manual/en/function.serialize< /a>
It doesn't sound like you need to use references/pointers at all. A pointer does not bind to a data value but rather a location in the memory. It is a mechanism to save memory or produce efficient code.
More simply put, a variable is like a house you can put things in, while a pointer is just the address.
If you have your PHP server pulling data from a MySQL database, and you want that data to be transferred intact to the client browser so it can be processed with JavaScript, then you have to put that data in an object and serialize it into JSON before sending it from the server.
The object could be the
mysqli_result
object that the MySQLi Database Extension outputs whenever you do a search, or you can create a custom data-holding class:Then you can use the PHP function
serialize
to turn that data into a JSON string.Reference: https://www.php.net/manual/en/function.serialize