K
K
Konstantin2016-01-28 22:30:52
PHP
Konstantin, 2016-01-28 22:30:52

How are objects passed in PHP by reference or by value?

Hello. I decided to take the PHP test and was surprised by the results. I kept thinking that objects are passed by reference, but in the test the answer is different. Here's a picture
c20908716efc4d669622ae07d6cf26e9.jpg
And taking this opportunity, another question related to multi-inheritance. Does PHP still support it or not? In other words, when asking such a question, do they mean traits or not?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wol_fi, 2016-01-28
@kostik34

in php4 objects were passed by value, in php5 they were passed by reference, hence the confusion.
There is no multiple inheritance in php (a class can only extend 1 class). Traits are a little different.

S
shekhovtsovy, 2021-03-03
@shekhovtsovy

I kept thinking that objects are passed by reference, but in the test the answer is different.

The objects themselves (or rather their identifiers) are passed by value (that is, they are copied in memory). Another thing is that these values ​​do not store the entire structure of objects, but only a pointer to the structure. That is why if you pass an object to a function through a parameter, and inside you do unset, it will still be available outside the function, but the attribute will change both inside and outside the function. This creates the misconception that objects are passed by reference. There is no error in the test here.
Read more here: https://www.php.net/manual/en/language.oop5.refere...
And taking this opportunity, one more question related to multi-inheritance. Does PHP still support it or not? In other words, when asking such a question, do they mean traits or not?

When asked about inheritance, it would be nice to clarify what is meant by class inheritance. Because PHP does have multiple inheritance. And these are not traits, but interfaces that allow you to inherit from 2 other interfaces at the same time. So the test is not entirely correct. And in the form in which it was asked, you answered correctly.
Read more here: https://www.php.net/manual/en/language.oop5.interf...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question