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

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,840 points
9,463 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,840 points
2,271 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
1,217 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
509 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,840 points
4,825 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,840 points
1,126 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,770 points

edited Jul 4, 2023 by Khan Sarfaraz 632 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
1,595 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
11,447 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