How do I create a copy of an object in PHP

0 votes

As far as I know, PHP objects are passed by reference.

<?php


class A {

    public $b;

}



function set_b($obj) { $obj->b = "after"; }


$a = new A();

$a->b = "before";

$c = $a; //i would especially expect this to create a copy.


set_b($a);


print $a->b; //i would expect this to show 'before'

print $c->b; //i would ESPECIALLY expect this to show 'before'


?>

How do I pass $a to set_b() by value, not by reference?

Jun 1, 2022 in PHP by Kichu
• 19,040 points
• 1,342 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The issue here is that in PHP 4 objects were passed by value and now in PHP 5 and above it's passed by reference that is why it had runtime pass by reference, which became deprecated.

Use the 'clone' operator in PHP5 to copy objects:

$objectB = clone $objectA;

I hope this helps you.

answered Jun 2, 2022 by narikkadan
• 86,360 points

edited Mar 5, 2025

Related Questions In PHP

0 votes
1 answer

How do I get the HTML code of a web page in PHP?

Hello @kartik, If your PHP server allows url ...READ MORE

answered Oct 6, 2020 in PHP by Niroj
• 82,800 points
• 11,379 views
0 votes
1 answer

How do I get the last inserted ID of a MySQL table in PHP?

Hello @kartik, If you're using PDO, use PDO::lastInsertId. If you're ...READ MORE

answered Oct 22, 2020 in PHP by Niroj
• 82,800 points
• 3,531 views
0 votes
1 answer

How do I strip all spaces out of a string in PHP?

If I have understood your question right, ...READ MORE

answered Feb 23, 2022 in PHP by Aditya
• 7,680 points
• 2,184 views
0 votes
0 answers

How do I replace part of a string in PHP?

I want to replace space in the first ...READ MORE

May 28, 2022 in PHP by Kichu
• 19,040 points
• 1,285 views
0 votes
1 answer

How do I convert a PDF document to a preview image in PHP?

Hello @kartik, You need ImageMagick and GhostScript <?php $im = new imagick('file.pdf[0]'); $im->setImageFormat('jpg'); header('Content-Type: image/jpeg'); echo ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,800 points
• 6,281 views
0 votes
1 answer

How do I recursively delete a directory and its entire contents files as well as sub dirs in PHP?

Hello @kartik, The user-contributed section in the manual ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,800 points
• 3,020 views
0 votes
1 answer

How do I copy an object in Java?

Create a copy constructor: class DummyBean { ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,730 points

edited Jul 4, 2023 by Khan Sarfaraz • 1,511 views
0 votes
0 answers
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
• 3,548 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
• 14,023 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP