PHP Object Access Syntax Question with the $

Posted by ImperialLion on Stack Overflow See other posts from Stack Overflow or by ImperialLion
Published on 2010-06-09T12:06:52Z Indexed on 2010/06/09 12:12 UTC
Read the original article Hit count: 217

Filed under:
|

I've been having trouble searching for this answer because I am not quite sure how to phrase it. I am new to PHP and still getting my feet on the ground. I was writing a page with a class in it that had the property name. When I originally wrote the page there was no class so I just had a variable called $name. When I went to encapsulate it in a class I accidental changed it to be $myClass->$name. It tool me a while to realize that the syntax I needed was $myClass->name. The reason it took so long was the error I kept getting was "Attempt to access a null property" or something along those lines. The error lead me to believe it was a data population error.

My question is does $myClass->$name have a valid meaning? In other words is there a time you would use this and a reason why it doesn't create a syntax error? If so what is the semantic meaning of that code? When would I use it if it is valid? If its not valid, is there a reason that it doesn't create a syntax error?

© Stack Overflow or respective owner

Related posts about php

Related posts about syntax