PHP
  Home arrow PHP arrow Page 2 - Magic Functions in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Magic Functions in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2009-05-26


    Table of Contents:
  • Magic Functions in PHP 5
  • Get and set functions: a quick overview
  • Property overloading in action
  • The set and get methods in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Magic Functions in PHP 5 - Get and set functions: a quick overview
    ( Page 2 of 4 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT