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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: