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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:30:41+00:00 2026-05-16T02:30:41+00:00

I use mysqli extension and bind the result to an object: class Item {

  • 0

I use mysqli extension and bind the result to an object:

class Item {
    public $id, $name;
    }
$Item = new Item;
$stmt->bind_result($Item->id, $Item->name);

Every call to $stmt->fetch() will overwrite $Item’s properties which became references. If I simply clone the object — these references remain and both instances change simultaneously:

object(Item)#1 (2) {
  ["id"]=>
  &int(1)
  ["name"]=>
  &string(4) "name"
}

So there should be a way to dereference them so clone really makes a copy, not a set of referenced data. Is there a simple, flexible way to retrieve all the data so it’s stored as an array of objects? The main problem is how to remove the references: &string.

The only solution I see is to instantiate another object and manually copy all the properties:

$Item2 = new Item;
foreach ($Item as $prop => $val)
    $Item2->$prop = $val;

but this seems to be an overkill: it will copy all properties (including some extra ones which were not actually referenced) which is not necessary, and moreover — is a penalty to the performance.

  • 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-16T02:30:41+00:00Added an answer on May 16, 2026 at 2:30 am

    You can implement your own __clone() magic function so that it will create an unreferenced’ copy of your object. I think it should work.

    http://www.php.net/manual/en/language.oop5.cloning.php

    <?php
    
    class Item {
      public $id, $name;
    
      public function __clone() {
        $id = $this->id;
        unset($this->id);
        $this->id = $id;
        $name = $this->name;
        unset($this->name);
        $this->name = $name;
      }
    }
    
    $id = 1;
    $name = 'o_0 Tync';
    
    $i1 = new Item();
    $i1->id = &$id;
    $i1->name = &$name;
    
    $i2 = clone $i1;
    
    $id = 2;
    $name = 'Mchl';
    var_dump($i1,$i2);
    

    Output

    object(Item)#1 (2) {
      ["id"]=>
      &int(2)
      ["name"]=>
      &string(4) "Mchl"
    }
    object(Item)#2 (2) {
      ["id"]=>
      int(1)
      ["name"]=>
      string(8) "o_0 Tync"
    }
    

    Ugly as hell, but works…

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

Sidebar

Related Questions

Can anyone instruct me on the proper way to use the MySQLi extension in
i'm trying to use mysqli to display data, but it displays nothing. what is
I have a user class that I need to use my database with. To
I use MySQL in a fairly complex web site (PHP driven). Ideally, there would
I'm about to use MySQL with Hibernate on a Tomcat 5.5.x server. Do I
What database should a beginner use in the C language? Can I use MySQL?
I have an ASP.net project I'm looking at and they want to use MySQL.
We are porting an app which formerly used Openbase 7 to now use MySQL
I used to use the standard mysql_connect(), mysql_query(), etc statements for doing MySQL stuff
Can MySql 5.0 views use tables that are located on another server? What is

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.