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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:33:07+00:00 2026-05-26T03:33:07+00:00

I am trying to turn an query result into classes. $result->setFetchMode(PDO::FETCH_CLASS, ‘myclass’, array()); This

  • 0

I am trying to turn an query result into classes.

$result->setFetchMode(PDO::FETCH_CLASS, 'myclass', array());

This works quite well however the class name myclass depends on the column values.

Is it possible to fetch each row and turn it into a different class depending on the rows values?


User example:

ID # name # age
1  # jon  # 12
2  # sue  # 23
3  # tom  # 24

I want to have all users with an age less then 21 to be instances of the class child.
Rows with age of 21 and above should be instances of the class adult.

So “jon” should be an instance of child.
“sue” and “tom” should be instances of adult.

  • 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-26T03:33:08+00:00Added an answer on May 26, 2026 at 3:33 am

    As you do not know the type (classname) of the returned objects before you do the query, you can not specify it.

    However, you could encapsulate that logic inside another type you use as return type which can then return the specific return type:

    /**
     * Should not have any private, public or protected members in it's definition.
     * 
     * Does only work for public properties.
     */
    class ReturnObject {
        public function getConcrete()
        {
            /* decide here which class */
           $classname = 'Child'; // or 'Adult'
    
           return $this->selfAs($classname);
        }
    
        private function selfAs($classname)
        {
            $l = strlen(__CLASS__);
            $s = sprintf('O:%d:"%s"', strlen($classname), $classname).substr(serialize($this), 5+strlen($l)+$l);
            $instance = unserialize($s);
            $instance->__construct();
            return $instance;
        }
    }
    

    You can then use the getConcrete() function on each returned object to return your specific type, your decision logic bound to the database return.

    Edit: I changed it into a version that will first initialize the objects properties via unserialize (please test if this works, it’s based on the assumption that we’re talking about public properties only and I don’t know if PDO just does the setters or more via reflection in the mode you’re using) and then calls the constructor function. The constructor needs to be public (and it must exist) so that this works.

    It’s technically possible to make this available for private and protected members as well, however this needs real reflection and it as well needs parsing of the serialized data as well. This class only renames the classname, but not inside private properties.

    However this is only one way for doing so. You probably only need a ->isChild() or ->isAdult() function on your Person class.

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

Sidebar

Related Questions

I'm trying to turn this query into a view: SELECT t.* FROM user t
I'm trying to turn some query results into click-able links through PHP. I'm a
Is it possible in Mysql to turn this query into 1 query ? UPDATE
I am trying to turn specific words or phrases into links with jQuery. So
I am trying to turn div#sidebar into a sidebar in my app. My code
I am trying to quickly invert the colors of an existing UIView (turn into
I'm trying to turn Etags off on iis 6 since I'm load ballancing multiple
I am trying to turn off Nagle's algorithm for a BSD socket using: setsockopt(newSock,
I am trying to turn off Request Validation for all action methods in a
I hope I don't lose anyone by mentioning Filemaker. I am trying to turn

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.