Getters, Setters and Magic Methods in OO PHP

Getters, Setters and Magic Methods in OO PHP

Introduction
Object Oriented Programming (OOP) is a very common concept in many of todays programming and scripting languages, while the benefits may be minimal in very small projects, it can remove the complexity in larger projects and can turn seemingly complex scripts into something much more manageable.

Creating accessor (getter and setter) methods
When we define a PHP class we need to provide all the necessary structure, this includes methods and properties to determine what information the object will store and how it will behave when asked to-do certain tasks.
One very common set of methods that programmers must create within their class definitions are the also known as . These methods are designed to force the user of the object to confirm to the object oriented paradigm .
The idea of encapsulation is to make sure that the properties of any object that we create are NOT directly accessible from outside the object. (refer to any object orientated programming guide to understand why this is considered bad).

Take the following basic class for example :-

Class AddressBook {

private $name;
private $email;

public function getName() {
return $this->name;
}

public function setName($newName) {
$this->name = $newName;
}

public function getEmail() {
return $this->email;
}

public function setEmail($newEmail) {
$this->email = $newEmail;
}

}

$myAddressBook = new AddressBook();
$myAddressBook->setName(“Fred Bloggs”);
$myAddressBook->setEmail(“”);
echo $myAddressBook->getName() . ” : ” . $myAddressBook->getEmail();

Since the two properties in this class are defined as private, this means that they are unable to be accessed from outside the object (or any objects that are created from a child class), so we need to provide some accessor methods so a user of the object can get and set these values, this is why we have created the accessor methods.

Magic Methods
Creating individual accessor methods for each property can be an acceptable way of managing the properties in a class definition, however PHP provides a more elegant way of achieving this using .

We can modify our class in the following way :-

Class AddressBook {

private $name;
private $email;

public function __get($name) {
return $this->$name;
}

public function __set($name, $value) {
$this->$name = $value;
}

public function saySomething() {
return ‘My name is ‘ . $this->name . ‘ and my e-mail address is ‘ . $this->email ;
}

}

$myAddressBook = new AddressBook();
$myAddressBook->name     = “Fred Bloggs”;
$myAddressBook->email     = “”;
echo $myAddressBook->name.” : “.$myAddressBook->email;

As you can see, the code is almost identical apart from the definition of the accessor methods, rather than providing an accessor method for each property we have used PHP’s magic methods to achieve the desired result. The main advantage here is that if the object had fifty properties that we needed to access, we would have to create a pair of accessor methods for each one, that’s an extra 100 methods! The magic methods are called when we attempt to access the object, depending on what you are trying todo determines which magic method is called, so for example, when we execute the command :-

$myAddressBook->name     = “Fred Bloggs”;

The magic method that is called is __set() and the appropriate variables are passed in as arguments to the method.  The first argument contains the property name and the second argument contains the value we wish to provide, in this case the $name argument would contain and the $value argument would contain .

If we attempted to access the object in the following way :-

echo $myAddressBook->email;

The magic method __get() would be called, and would be passed the name of the property that we are attempting to retrieve, in this case it would be .

Conclusion
Using magic methods to create your getters and setters is a significant time saver, and even though the examples above are very basic, you can clearly see the potential time they save.

This entry was posted in Managed Hosting. Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>