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 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

So, I started to use MySQLi extension as I heard it will be the
Can anyone instruct me on the proper way to use the MySQLi extension in
I am starting to use MySQL with JDBC. Class.forName(com.mysql.jdbc.Driver); conn = DriverManager.getConnection(jdbc:mysql:///x, x, x);
I'm currently using the mysqli php extension. Traditionally I have used mysqli_real_escape_string to escape
For fun I am replacing the mysqli extension in my app with PDO. Once
This question is for Mysql geospatial-extension experts. The following query doesn't the result that
I get the following error: Fatal error: Class 'mysqli' not found in my php
I'm trying to dynamically bind mysql_stmt parameters and get the result in an associative
I am wanting to use the MySQL spartial extension for a project. What it
I am trying to use the Database mailer extension for my Radiant CMS application.

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.