开发者

Is it redundant to have a PHP object mirror a Javascript object, for database interactivity?

开发者 https://www.devze.com 2023-02-10 23:53 出处:网络
I\'m immer开发者_开发技巧sing 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 immer开发者_开发技巧sing 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)
    }
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号