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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:38:26+00:00 2026-06-14T11:38:26+00:00

the PHP mysqli_result class implements the Traversable interface for some time now. This gives

  • 0

the PHP mysqli_result class implements the Traversable interface for some time now.
This gives some interesting possibilities like the following piece of code:

<?php

$db = new mysqli( '...' );

$result = $db->query( '...' );

foreach( $result as $row ) {
   //$row is an associative array containing the fetched values
}

Now, I like this pretty much, but in the library I am currently working on I would like to have objects fetched, not associative arrays. I also want to be able to pass a class to it, so I can create a simple model system with it

For that I extend it like this:

<?php

class mysqli_result_extension extends mysqli_result {

    protected $result_class;

    public function set_result_class( $class ) { $this->result_class = $class; }

    public function function_to_overwrite() {

        return $this->fetch_object( $this->result_class );
    }
}

My problem and my question to you:

How do I switch from fetch_assoc to fetch_object now?
I would like to overwrite the method the Traversable interface uses in order to return the result, but it doesn’t define any (http://php.net/manual/en/class.traversable.php)

So what would I need to overwrite in order to switch from fetch_assoc to fetch_object and to be able to pass my $result_class to it so I am able to do something like this:

<?php

$db = new mysqli_extension( '...' );

$posts = $db->query( 'select * from posts' );
//$result is object of type mysqli_result_extension

$posts->set_result_class( 'post' );

foreach( $posts as $post ) {
   //$post is an instance of class 'post'
}

I don’t want something dirty, hacky like overwriting fetch_assoc and just returning the object in it

Thanks in advance for your help and input

  • 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-06-14T11:38:28+00:00Added an answer on June 14, 2026 at 11:38 am

    As I stated in the comments, it’s easy to do exactly what you want with PDO. Just set the PDO::ATTR_DEFAULT_FETCH_MODE attribute, and it will do the appropriate kind of fetch when you do the foreach loop.

    You can also set the PDO::ATTR_STATEMENT_CLASS attribute to define the class to use.

    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);
    $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS,array('classname',$constructorArgs));
    

    See http://php.net/manual/en/pdo.setattribute.php for more info.

    With that solution, you’d need to change the ATTR_STATEMENT_CLASS attribute for each query, depending on what class you want to instantiate.

    Alternatively, you can set it to specify the classname within the query:

    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE);
    $stmt = $db->query("SELECT 'classname', * FROM `table` WHERE `id` = 1;");
    

    Simply add the relevant classname to your query as the first field to be selected.

    See one of the answers on this related question In PHP, How do I set default PDO Fetch Class? for more on this.

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

Sidebar

Related Questions

i've implemented this pagination class for search.php page in a separate file called class.pagination.php
So I have this database class in PHP and I only have 1 function
<?php class ann { public function __construct($context, $orgs_id, $created_at) { $this->context = $context; $this->orgs_id
Can a class extend both an interface and another class in PHP? Basically I
I have the following PHP code in a class extending mysqli. $queryString = 'SELECT
I'm noticing some strange results with date conversions between MYSQL and PHP. Please see
I just started using mysqli API for PHP. Apparently, every time an object of
I have extended PHP's mysqli class, which works fine. But how can I make
I have a MySQL singleton class written in PHP. Its code is listed below:
I have a html page that calls a php object to get some data

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.