When trying to change it,throw an exception.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I suppose a solution, for class properties, would be to :
__getmethod to access that property, using the “fake” name__setmethod so it throws an exception when trying to set that property.For variables, I don’t think it’s possible to have a read-only variable for which PHP will throw an exception when you’re trying to write to it.
For instance, consider this little class :
Instanciating the class and trying to read the property :
Will get you the expected output :
While trying to write to the property :
Will get you an Exception :