Assigning an object to a var then overwriting that object with new

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
phaseflow
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Jan 10, 2020 9:55 am

Fri Jan 10, 2020 10:11 am

I'm working with code like this (PHP 5):

Code: Select all

$serviceFeeObject = new ServiceFeeObject;
$serviceFeeObject->Amount = $someCalculatedAmount;
$serviceFeeCalculatedObject = $serviceFeeObject;

if ($isTaxed) {
	$serviceFeeObject = new ServiceFeeObject;
	$serviceFeeObject->Amount = $someCalculatedTaxAmount;
	$serviceFeeTaxedObject = $serviceFeeObject;
}
However, I thought PHP passed an object from one variable to another by reference, meaning when $serviceFeeObject was instantiated again (as new ServiceFeeObject) if $isTaxed is true, $serviceFeeCalculatedObject will be wiped as it was assigned by reference to $serviceFeeObject;

I haven't tested or debug'd it yet (I'm not even sure this code is used in the bloated randomly-deprecated mass I'm working with), but how should I be understanding this?
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Fri Jan 10, 2020 12:03 pm

It will depend on which version of PHP you are using, how the class has been defined and then if you intend to update to a more recent version (which may or may not alter its behaviour).

Unfortunately, you will have to test, test and test again to make sure that you are getting the behaviour that you expect - as noted though, this can be version dependant.
phaseflow
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Jan 10, 2020 9:55 am

Mon Jan 13, 2020 1:14 pm

I specified it's PHP 5, and if you know how it will behave depending on how it's set up, please specify how under the scenarios that you're aware of...
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Tue Jan 14, 2020 8:45 am

PHP 5 change log covers PHP 5.0.0 to PHP 5.6.40

As specified, it depends which version you are using and if you intend to update it.
phaseflow
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Jan 10, 2020 9:55 am

Tue Jan 14, 2020 10:11 am

hyper wrote:
Tue Jan 14, 2020 8:45 am
PHP 5 change log covers PHP 5.0.0 to PHP 5.6.40

As specified, it depends which version you are using and if you intend to update it.
Well, I was hoping you'd give what you knew based on your experience, and posting the PHP 5.x versions and changelog isn't really helping unless you want to point out where the question is answered. It doesn't matter if I intend to update, it matters what I'm asking about now, and I already stated the version, PHP 5, but if you really want to know the sub-version, it's 5.5... does that change your answer? If not, then there was no point in knowing.

I'd really prefer if someone who knew the answer or could at least give what they think or their experience would answer, or just no answer, rather than round-about answers or answers to questions I didn't ask.

I'll scour the change logs to see if they even answer the question, or just run the tests myself... I thought it would be better to ask people who might actually know.
Post Reply