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 7594195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:21:19+00:00 2026-05-30T21:21:19+00:00

I am wondering whether or not it is possible to elegantly map the results

  • 0

I am wondering whether or not it is possible to elegantly map the results of a PDO query to an array member in a class rather than have them floating about as public properties of that object.

Say I have the (condensed) following:

class DBObject {

    protected
        $record = array();

    function __construct(array $record) {
        if(!empty($record)) {
            $this->loadRecord($record);
        }
    }
}

Ideally, I want to call the constructor with an array of values passed from the database, rather than use __set or any other weird methods. So using PDO’s existing API would be great.

My rough get_all function at the moment has got this far:

static function get_all() {
    $class = get_called_class();
    $results = DB::factory()->query('SELECT * FROM ' . $class . ' ORDER BY ID');
    $results->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, $class);
    return $results;
}

NB: I’m running PHP 5.3 and MySQL through PDO, and already know this problem is solveable using __set, but I explicitly want to avoid using it in favour of something more performant.

  • 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-30T21:21:20+00:00Added an answer on May 30, 2026 at 9:21 pm

    Removed previous code


    Right, can’t you do something like this:

    class DBObject {
    
        protected $record = array();
    
        function __construct($record = null) {
            if(null === $record){
                $obj_vars = get_object_vars($this);
                $cls_vars = get_class_vars(get_class($this));
                $this->$record = array_diff_key($obj_vars, $cls_vars);
            }else{
                $this->record = $record;
            }
        }
    }
    

    The problem with this however is that the values are still available as public members.
    But what it will do is compare ‘pre-defined’ (class) members to the actual (object) members.

    Since PDO will create new members in the object you can use array_diff_key to get the ‘new’ members.

    Yes, this will still not pass them through your constructor.

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

Sidebar

Related Questions

I am wondering whether or not it is possible to output the class name
I was wondering how to check whether a variable is a class (not an
I've been wondering whether it is possible or not to pass a function as
I was wondering whether it is possible to have 2 instances of the camera
I was wondering whether or not it is possible to use the NSCoder method:
I have two tables, I am wondering whether is it possible to write a
I was wondering whether or not I can extend the Enum type in C#
This might be a stupid question, but I was wondering whether or not you
I'm wondering whether something like this is possible (and relatively easy to do), and
I am just wondering whether it is possible to sort by 'combined columns' now

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.