How do you add an attribute to an Object in PHP?
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.
Well, the general way to add arbitrary properties to an object is:
You can, much cleaner, pre-define attributes in your class (PHP 5+ specific, in PHP 4 you would use the old
var $attributename)this is highly recommended, because you can also document the properties in your class definition.
You can also define getter and setter methods that get called whenever you try to modify an object’s property.