开发者

Should I be using an object for this JS/PHP interaction? Does this look right?

开发者 https://www.devze.com 2022-12-18 10:20 出处:网络
Part of a site I am building uses a javascript ajax request to hit a PHP script that retrieves, parses, sorts and paginates and returns results from an XML file. I have done this in a pretty straight

Part of a site I am building uses a javascript ajax request to hit a PHP script that retrieves, parses, sorts and paginates and returns results from an XML file. I have done this in a pretty straight forward fashion on the PHP side with variables POST variables being retrieved, sanitized and reassigned, SimpleXML loading the file and using an xpath query to grab the needed bits, sorting handled by the SPL's LimitIterator, etc.

What I am wondering is if this should be wrapped up in an object and, perhaps most importantly, how this object should be constructed.

The code is used, with fairly minor changes (like the sorting direction, the specific xpath query and the particular XML file) for a number of different requests in different parts of the site so I suspect that perhaps turning it into a reusable object might be the way to go? What do you think? Should I watch out for anything? Any special way this should be approached (like, does it need to be a Singleton or can I pass the POST variables into a constructor and then have that separate them out into their various reassigned variables)? Basically I am wondering if I am on the right path with doing this in an object oriented manner and I was thinking something like this (this is meant to be a pseudo code sort of example and my use of some things is off, I know. Please make suggestions for improvement like am I using $this->foo properly or am I using public/private properly?):

        class GetXMLData() {
            public $start;
            public $end;
            public $xmlfile; 
            public $limited;
            private $results;


            public function __constructor($_POST, $xmlfile) {
            $this->start = sanitize($_POST['start']);
            $this->end = sanitize($_POST['end']);
            $this->xmlfile = $xmlfile;
            }

            //load the xml file
            private function loadFile($this->file) {
                return simplexml_load_file($this->开发者_StackOverflow社区;file);
            }

            private function sorting(){
                // an internal sorting function that could use loadFile() perhaps and then return $results to the pagination function?
            }

            private function paginate($this->results) {
                $this->limited = new LimitIterator(new ArrayIterator($this->results), $start, $end);
                return $this->limited
            }

        }   

    $stuff = new GetXMLData($_POST, $xmlfile);
    return $stuff;


It looks fine to me, but I definitely would not make it a singleton. This seems to be a common misconception with the singleton pattern: it's not for use when you think you will only need one of an object, it's for when have multiples of that object would cause problems, and you need to guarantee there is over only one instance of it.

also, I would probably make all of the variables private, and use getters and setters for whenever you need access to them (if you need access to them).


Objects have nothing to do with POST, or interacting with JavaScript, or any of that. They are simply a way of organizing code on the server.

0

精彩评论

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

关注公众号