PHP
  Home arrow PHP arrow Page 3 - Abstract Classes in PHP: Working with ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Abstract Classes in PHP: Working with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · If you've been reading the previous articles of this series, probably you'll find...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT