Home  >  Q&A  >  body text

How to access properties inside PHP?

<p>Follow up based on this document: https://www.php.net/manual/en/language.oop5.references.php</p> <blockquote> <p>A key point of PHP object-oriented programming that is often mentioned is that "objects are passed by reference by default." This is not entirely correct. </p> </blockquote> <blockquote> <p>In PHP, object variables do not contain the object itself as a value. It simply contains an object identifier that allows object accessors to find the actual object. </p> </blockquote> <p>How does this actually work? For example, in C, the arrow operator seems to implicitly dereference the pointer and then access the property as if it were accessed on the object variable itself. </p> <p>This is how I understand it:</p> <pre class="brush:php;toolbar:false;">obj->prop (*obj).prop // Equivalent to the line above</pre> <p>This looks very neat. In both cases, the property call is the sum of the object variable address and the property offset. </p> <p>But how does this work in PHP? The documentation suggests that a pointer does not store a memory address, but rather an "object identifier". Is accessing properties in PHP a highly abstract process, or do you resolve the object identifier into a memory address and then access it similar to C/Java etc.? </p>
P粉518799557P粉518799557383 days ago495

reply all(1)I'll reply

  • P粉600845163

    P粉6008451632023-09-06 11:33:11

    This is a highly abstract process, and the similarity in syntax does not mean that the code will "fall into" working like C/C. You can dig into the code to see how it works under the hood.

    reply
    0
  • Cancelreply