PHP tip 8: Object-oriented style “getter” and “setter” functions example

How to implement getter and setter functions (a standard oop paradigm) in PHP classes (with a short, commented example).

[et_pb_section fb_built=”1″ _builder_version=”3.0.93″][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.0.93″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_image src=”” alt=”PHP getter and setter functions” title_text=”PHP getter and setter functions” align=”center” _builder_version=”3.7″ custom_margin=”||5px|” custom_padding=”|||”][/et_pb_image][/et_pb_column][/et_pb_row][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.0.93″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″]


[/et_pb_text][et_pb_text _builder_version=”3.7″]


[/et_pb_text][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″]


[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||”]


A standard paradigm in object-oriented programming is to use getter and setter methods (class functions) to provide access to the class’ attributes (variables).

In PHP classes too you can easily implement getter and setter methods.


Why should you use a method to retrieve or to set a class’ variable instead of directly accessing it?

It’s quite simple.

If you set a class’ attribute as public, then you will have no control over which values the attribute can take. Any external code will be able to change it without any constraint.


If you, instead, set the property as private and provide a public method to set its value, you can decide which values to accept and you can perform any type of validation and sanitization before setting the new value. 

In other words, you can implement a filter between the attribute and the code outside of the class.


In the following short example, you can see how to implement getter and setter functions in PHP.

In this example, the $name attribute must be between 3 and 16 characters long. The setter function makes sure that the new name’s length is within that range before changing the attribute:

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row custom_padding=”27px|0px|27px|0px” _builder_version=”3.5.1″][et_pb_column type=”4_4″ _builder_version=”3.0.47″ parallax=”off” parallax_method=”on”][et_pb_text _builder_version=”3.7″ text_font=”||||||||” text_font_size=”13px”]


class MyClass
	/* Private attribute, cannot be accessed directly */
	private $name;
	/* Constructor */
	public function __construct()
		$this->name = '';
	/* Getter function to read the attribute */
	public function get_name()
		return $this->name;
	/* Setter function to change the attribute */
	public function set_name($new_name)
		if ($this->is_valid_name($new_name))
			$this->name = $new_name;
	/* Checks if the name is valid */
	private function is_valid_name($name)
		$valid = TRUE;
		/* Just checks if the string length is between 3 and 16 */
		if (mb_strlen($name) < 3)
			$valid = FALSE;
		else if (mb_strlen($name) > 16)
			$valid = FALSE;
		return $valid;

$mc = new MyClass();
echo $mc->get_name();

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”|||”]



If you have any questions, feel free to ask in the comments below or on my Facebook Group: Alex PHP café.

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

[/et_pb_text][et_pb_text _builder_version=”3.2.2″]



[/et_pb_text][et_pb_code _builder_version=”3.10.1″][/et_pb_code][/et_pb_column][/et_pb_row][/et_pb_section]

Leave a Reply

Your email address will not be published. Required fields are marked *