As I expressed in the introduction, the first two magic functions that I plan to cover are the popular "__set()" and "__get()" methods. They can be really useful for overloading properties of a class. But, at this point, you may be wondering how to use these functions. Well, as with any other magic methods, the "__set()" and "__get()" functions have no concrete implementation and must be given a explicit definition. In the case of the "__set()" function, it'll be called automatically by the PHP engine each time a script attempts to assign a value to a property of a class that hasn't been explicitly declared. On the other hand, the "__get()" function will be invoked transparently when a script tries to retrieve the value of an undeclared class property as well. This setting and retrieval process of values of properties within a class that have no a explicit declaration comprises what's known as "property overloading." It must be very carefully implemented to prevent its misuse and abuse. Does this sound a bit confusing to you? Fear not; in a moment, I'm going to back up all of these concepts with a couple of examples that will illustrate how to use the team of "__set()" and "__get()" functions. Say that there's a basic class called "User," which represents, through software, a real-world user. The definition of this sample class would be something like this: class User { private $fname = 'Alejandro'; private $lname = 'Gervasio'; private $email = 'alejandro@mydomain.com';
// constructor (not implemented) public function __construct(){}
// set user's first name public function setFirstName($fname) { $this->fname = $fname; }
// get user's first name public function getFirstName() { return $this->fname; } // set user's last name public function setLastName($lname) { $this->lname = $lname; }
// get user's last name public function getLastName() { return $this->lname; } // set user's email address public function setEmail($email) { $this->email = $email; }
// get user's email address public function getEmail() { return $this->email; } } As you can see, the structure of the above "User" class is pretty easy to follow. It's only composed of a few setters and getters, used for setting and retrieving the values assigned to its three declared properties, that is $fname, $lname and $email respectively. So far, nothing strange is happening here, right? What follows is a simple demonstration of how to use this class: $user = new User(); $user->setFirstName('John'); $user->setLastName('Doe'); $user->setEmail('john@domain.com');
// display user data echo 'First Name: ' . $user->getFirstName() . ' Last Name: ' . $user->getLastName() . ' Email: ' . $user->getEmail();
/* displays the following First Name: John Last Name: Doe Email: john@domain.com */ Well, that was extremely boring, wasn't it? All that the previous script does is create an instance of the "User" class, assign some values to the properties of the class via the pertinent setters, and finally display the values in question using the pertinent getters. However, this tame and well-known scenario is about to become more exciting. It's possible to redefine the "User" class by using the "__set()" and "__get()" magic functions to make the class's signature much shorter. At the same time, they'll allow us to assign undeclared properties dynamically. The full details of this process will be discussed in the section to come. Click on the link below and keep reading to learn more.
blog comments powered by Disqus |
|
|
|
|
|
|
|