PHP
  Home arrow PHP arrow Page 3 - Abstract Classes in PHP: Working with 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? 
Google.com  
PHP

Abstract Classes in PHP: Working with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 31
    2006-02-08


    Table of Contents:
  • Abstract Classes in PHP: Working with PHP 5
  • Working with a highly improved object model: defining abstract classes in PHP 5
  • Calling class methods out of the object context: using the scope resolution operator
  • Using abstract classes in PHP 5: setting up an example

  • 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


    Abstract Classes in PHP: Working with PHP 5 - Calling class methods out of the object context: using the scope resolution operator
    ( Page 3 of 4 )

    As I explained before, you can declare a class abstract, and possibly even call their methods without raising a fatal error. Of course, this process is rather pointless and unusual, but for the sake of completeness, here’s how I’d call the methods of the sample abstract class you saw right at the beginning of the article, by using the scope resolution operator:

    abstract class Message{
        private static $message;
        public function setMessage($message){
            if(!is_string($message)){
                throw new Exception('Invalid parameter type!');
            }
            self::$message=$message;
        }
        public function fetchMessage(){
            return self::$message;
        }
    }
    try{
        // call 'setMessage()' method out of object context
        Message::setMessage('This is an abstract PHP 5 class, and its
    methods are called out of the object context.');
        echo Message::fetchMessage();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    As you can see, the above example uses the double colon operator (also called Paamayim Nekudotayim, or double-colon in Hebrew), in order to call the class methods without raising a fatal error. However, in most cases abstract classes must include abstract methods, which turns the above snippet into a rather inefficient method for using an abstract class, since all its methods are explicitly implemented.

    At this stage, I hope you understand how to define an abstract class in PHP 5. However, all the theory that you just learned is rather useless if I don’t show you a concrete case. It’s precisely for this reason that over the next few lines, I’ll set up an illustrative example. Thus you can grasp the concepts for using abstract classes in PHP 5. Click the link and keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek