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

  • Home
  • SEARCH
  • 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 827701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:36:52+00:00 2026-05-15T03:36:52+00:00

Lets say I have a class like this: Class User { var $id var

  • 0

Lets say I have a class like this:

Class User {
  var $id
  var $name;
}

And I run a query using PDO in php like so:

$stm = $db->prepare('select * from users where id = :id');
$r = $stm->execute(array(':id' => $id));
$user = $r->fetchObject('User');

If I vardump my user object it has all kinds of other fields in it that I have not defined in the User class. Obviously I could make my query specific so that it only gives me back the fields I need/want. But if I don’t want to do that is there any way to make this work the way I want it to?

I like the idea of fetchObject, because it’s one line of code to create this object and set member variables for me. I just don’t want it to set variables I haven’t defined in my class.

EDIT:

Well it seems like karim79 is right and the fetch or fetchObject won’t work the way I want it to. I’ve added the following bit of code after I do the fetch to get the desired results.

$valid_vars = get_class_vars('User');
foreach (get_object_vars($user) as $key => $value) {
    if (!array_key_exists($key, $valid_vars)) {
        unset($user->$key);
    }
}

Obviously not the most elegant solution :/ I’m going to extend the PDOStatement class and add my own method fetchIntoObject or something like that and automatically do these unsets. Hopefully shouldn’t be to much overhead, but I want to be able to easily fetch into an object with 1 line of code 🙂

SUPER EDIT:

Thanks to mamaar’s comment I went back to the documentation again. I found what the problem is. http://us.php.net/manual/en/pdo.constants.php and scroll down to PDO::FETCH_CLASS and it explains that the magic method __set() is used if properties don’t exist in the class. I overwrote the method in my target class and tada, works. Again, not the most elegant solution. But now I understand the WHY, and that’s important to me 😀

  • 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-15T03:36:52+00:00Added an answer on May 15, 2026 at 3:36 am

    I don’t think that’s possible. fetchObject will create an instance of the classname specified as fetchObject‘s $class_name parameter (which defaults to stdClass). It will not check for existing classes with the same name and create an instance, assigning values only to member variables which match column names in the result. I would suggest relying on something more boring, like this:

    $user = new User($result['id'], $result['name']);
    

    Which would of course mean giving your User class a constructor:

    Class User {
      var $id
      var $name;
    
      public function __construct($id, $name)
      {
         $this->id = $id;
         $this->name = $name;
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 403k
  • Answers 403k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The second argument to dict.fromkeys is just a value. You've… May 15, 2026 at 5:18 am
  • Editorial Team
    Editorial Team added an answer By default, widgets in a TableRow have android:layout_width="wrap_content". That does… May 15, 2026 at 5:18 am
  • Editorial Team
    Editorial Team added an answer Make sure you set the sectionNameKeyPath when instantiating your fetchedResultsController… May 15, 2026 at 5:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.