Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 566891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:01:32+00:00 2026-05-13T13:01:32+00:00

Part of a site I am building uses a javascript ajax request to hit

  • 0

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->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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:01:32+00:00Added an answer on May 13, 2026 at 1:01 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my (PHP) web app, I have a part of my site that keeps
I'm building a simple PHP web app that uses Paypal to add credit to
We have a site that needs to (as part of our process) generate a
Part of our java application needs to run javascript that is written by non-developers.
I'm part of a team building an ADO.NET based web-site. We sometimes have several
I'm building a site for a client that needs to support image uploads (an
I'm looking for a PHP function to close HTML tags. I'm building a site
I'm building a web site (asp.net) for a company that manufactures small customizable anodized
I am building an AJAX powered website which uses the Really Simple History (RSH)
I'm building an iPhone app that, in part, allows the user to log in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.