Concept

How do accessor and mutator methods provide access to private member variables?

ComputerScienceOne / Accessor & Mutator Methods

"Since we have made all the member variables private, no code outside the class may access or modify their values. It is generally good practice to make member variables private to restrict access. However, if we still want code outside the object to access or mutate (that is, change) the variable values, we can define accessor and mutator methods (or simply getter and setter methods) to facilitate this. Each getter method returns the value of the instance’s variable while each setter method takes a value and sets the instance’s variable to the new value. It is common to name each getter/setter by prefixing a get and set to the variable’s name using lower camel casing. For example:\n\n1 public function getFirstName() {\n\n2 return $this->firstName;\n\n3 }\n\n4\n\n5 public function setFirstName($firstName) {\n\n6 $this->firstName = $firstName;\n\n7 }"

Related Ideas

How do accessor and mutator methods provide access to private member variables? | ComputerScienceOne | Bifalgorithm | Bifalgorithm