I just realize I’ve been doing that and it seems like the objects’ properties are still being set when I use
object->set($property, $value);
What is the expected behavior in this situation?
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.
set is just an method of the object’s object here, probably you are referring to setter method. which takes two arguments and pass it to the class property. the behavior of setting the class depends on the code of set method, there are some benefits of using this method.
there are two ways for storing the data in a class, one way is to directly assign the values to class property for example
if you need to assign the values to the class property you would do like.
now the class property $a will contain the value hello and the class property $b will contain the value world, however there is a problem to this approach, we can never tell the user what type of data should come in. if we need to store only integers in $a there is no way we can do that. to overcome this you can use a setter meth, wherein we declare the class property as protected or private, and set the value through a class method.
you can simply google about getter and setter method and get more information.
class constructor is nothing but an method which is invoked automatically when you instantiate an object. consider that you want to instantiate an object by forcing the user to provide the user id first. or may be you want to do some useful calculation before other methods are called. this is where class constructor comes into picture. consider the below example
so when you instantiate the object
this will result into error. since you haven’t provided the userId. so the correct way is.
now the class property
$userIdwill hold the value one upon the instantiation of the object.hope this helps you.