Home arrow PHP arrow Page 2 - Magic Functions in PHP 5

Get and set functions: a quick overview - PHP

Itís not breaking news that the release of PHP 5 drastically changed the way that many developers build their web-based programs. The incorporation of a much more robust object model, along with the introduction of native exceptions, type hinting and so forth (add your own improvement to the list) has given the language the maturity that we see in it today. This seven-part article series will explain an important new feature: magic functions.

  1. Magic Functions in PHP 5
  2. Get and set functions: a quick overview
  3. Property overloading in action
  4. The set and get methods in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
May 26, 2009

print this article



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();





// 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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: