为了数据库交互性,让 PHP 对象镜像 Javascript 对象是否多余?
我正在沉浸在 PHP 和 Javascript 中,但我对编码方法和概念仍然很陌生;我正在寻找有关我的暂定方法的一些反馈。
我正在构建一个应用程序,用户在其中注册,然后获得“节点创建”的访问权限。我想保存每个用户的节点名称和位置,以便他们可以登录并查看节点的确切位置(使用正确的名称)。
我的问题:
“我计划拥有 2 个相同的 对象,一个在 Javascript 中,以及 PHP 中的其他...但我意识到,这个 可能是多余的;如果我需要的话 所做的是将数据从 数据库到Javascript对象,是 没有必要使用 PHP“克隆” 作为中间人的对象?”
我的想法是,它可能更容易管理(是的,OOP),但正如我所说,我是应用程序开发的新手,并且希望获得有关此事的一些反馈。PHP
对象示例:
class Node {
public $name; // Stored JS object name
public $position; // Stored JS object position
function setObject() {
// set JS object name on app load
// set JS object position on app load
}
}
?>
JavaScript 对象示例:
node = {
name : $name; // set name
position : $position; // set position
findPosition : function() {
// Find JS object's updated position (when moved)
}
}
I'm immersing myself in PHP and Javascript, but I'm still new to coding methodology and concepts; I'm looking for a little feedback on my tentative approach.
I'm building an application where a user signs up, and then gains access to "node creation." I want to save the node name and position per user, so they can log in and see there nodes exactly where they left them (with the correct names.)
My question:
"I was planning on having 2 identical
objects, one in Javascript, and the
other in PHP... but I realized, this
might be redundant; if all I need to
do is transfer the data from the
database to the Javascript object, is
it unnecessary to use a PHP "clone"
object as a middle-man??"
My thoughts were that it might be simpler to manage (yay OOP,) but as I said, I'm new to application development and would love some feedback on the matter.
PHP object example:
class Node {
public $name; // Stored JS object name
public $position; // Stored JS object position
function setObject() {
// set JS object name on app load
// set JS object position on app load
}
}
?>
Javascript object example:
node = {
name : $name; // set name
position : $position; // set position
findPosition : function() {
// Find JS object's updated position (when moved)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的想法是错误的。
您将 PHP 对象称为“中间人”。在 Web 应用程序中,中间人是 HTTP 连接。这里的中间人是数据传输格式(JSON、XML、自定义格式等)。对于客户端脚本来说,数据格式化的完成方式并不重要,并且服务器也不关心客户端对代码执行的操作。
您应该问的真正问题是您在服务器端需要什么。服务器端脚本的目的是将数据库中的数据转换为发送到客户端所需的格式,并可能修改此数据以响应客户端。您需要决定服务器端数据将具有哪些类型的方法和属性,并围绕其设计代码。不要尝试复制客户端代码,因为它有完全不同的目的。
I think you're conceiving this the wrong way.
You are calling the PHP object the "middle man". In a web application, the middle man is the HTTP connection. The middle man here is whatever the data transfer format is (JSON, XML, something custom, whatever). It doesn't matter for your client-side scripting how the data formatting was done, and the server doesn't care what the client does with the code.
The real question you should be asking is about what you need on the server-side. The purpose of the server-side script is to transform the data in your database into the format needed to send to the client, and to possibly to modify this data in response to the client. You need to decide what kind of methods and properties your server-side data will have and design your code around that. Don't try to replicate the client-side code, because it has an entirely different purpose.
您只需镜像与数据库操作相关的位。请记住,Javascript 是一个客户端系统。它可以通过 AJAX 调用与服务器端 PHP 脚本进行交互,但 PHP 脚本只会在请求期间处于活动状态,然后再次关闭。
因此,您必须每次请求时都传输整个对象,否则 PHP 端将无法与 Javascript 端同步。
You'd only have to mirror the bits that are relevant to the database action. Remember that Javascript is a client-side system. It can talk back to the server-side PHP scripts via AJAX calls, but the PHP script will only be active for the duration of the request and then shut down again.
As such, you'd have to transfer the entirety of your object on each request, each time, or the PHP-side wouldn't be in sync with the Javascript-side.