简体   繁体   中英

How does PHP access properties internally?

Following up on this documentation: https://www.php.net/manual/en/language.oop5.references.php

One of the key-points of PHP OOP that is often mentioned is that "objects are passed by references by default". This is not completely true.

In PHP, an object variable doesn't contain the object itself as value. It only contains an object identifier which allows object accessors to find the actual object.

How does this actually work? In C++ for example it seems that the arrow operator implicitly dereferences the pointer and then accesses the properties like when accessing them on the object variable itself.

Here's what I mean:

obj->prop
(*obj).prop // equivalent to line above

This seems pretty clean. The property is called as the sum of the object-variable-address and the property-offset in both cases.

But how does that work in PHP? The documentation suggests that the pointer does not store the memory address but rather an "object-identifier". Is accessing properties in PHP a highly abstracted process or does it resolve the object-identifier for the memory address and then access it in a similar way to C++ / Java / etc.?

It's a highly abstracted process, similarity in syntax does not indicate that the code "falls through" to working like C/C++. You can dive into the code to see how it works under the covers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM