PHP
  Home arrow PHP arrow Page 3 - PHP 5 Helpers: Calling Methods Out of Object Scope
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 
iPad news and developer info.
Ads by affinity 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
VPS Hosting 
Forums Sitemap 
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

PHP 5 Helpers: Calling Methods Out of Object Scope


By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-07-27


    Table of Contents:
  • PHP 5 Helpers: Calling Methods Out of Object Scope
  • Review: building a text helper class
  • Working with static methods
  • The new text helper class 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


    Summary: Theoretically defining the role of helper classes in PHP 5 (and other popular server-side scripting languages) is a pretty approachable process. As their name suggests, helper classes provide developers with a set of logically-grouped methods that allow them to easily perform certain repetitive tasks that are common to different web applications. This is the second part of an eight-part article series that shows you how to build a variety of useful helper classes and expand on their functionality.

    PHP 5 Helpers: Calling Methods Out of Object Scope - Working with static methods
    (Page 3 of 4 )

    The best way to take advantage of the functionality given by the previous text helper class without dealing with its incidental instantiation is declaring its methods static.

    To do this, I知 going to subtly modify the definition of the class in question, which now will look as follows:

    class TextHelper

    {

    // constructor not implemented

    public function __construct(){}

     

    // convert new lines to '<br />' tags

    public static function newline_br($str)

    {

    if (is_string($str) AND empty($str) === FALSE)

    {

    return nl2br($str);

    }

    }

     

    // convert new lines to '<p>' tags in string

    public static function newline_par($str, $id = '', $class = '')

    {

    if (is_string($str) AND empty($str) === FALSE)

    {

    if ($id != '')

    {

    $id = ' id="' . $id . '"';

    }

    if ($class != '')

    {

    $class = ' class="' . $class . '"';

    }

    return '<p' . $id . $class . '>' . str_replace("n", '</p><p>', $str) . '</p>';

    }

    }

     

    // convert new lines to 'div' tags in string (id and class attributes can also be specified)

    public static function newline_div($str, $id = '', $class = '')

    {

    if (is_string($str) AND empty($str) === FALSE)

    {

    if ($id != '')

    {

    $id = ' id="' . $id . '"';

    }

    if ($class != '')

    {

    $class = ' class="' . $class . '"';

    }

    return '<div' . $id . $class . '>' . str_replace("n", '<div></div>', $str) . '</div>';

    }

    }

     

    // uppercase all characters in string

    public static function uppercase_all($str)

    {

    if (is_string($str) AND empty($str) === FALSE)

    {

    return strtoupper($str);

    }

    }

     

    // lowercase all characters in string

    public static function lowercase_all($str)

    {

    if (is_string($str) AND empty($str) === FALSE)

    {

    return strtolower($str);

    }

    }

    // uppercase first character in string

    public static function uppercase_first($str)

    {

    if (is_string($str) AND empty($str) === FALSE)

    {

    return ucfirst(strtolower($str));

    }

    }

    }

    If you've frequently worked with static methods, then you値l quickly grasp the modified signature of the text helper class. Most of its source code remains practically the same, except for the static declaration of all of its methods (apart from the constructor).

    Even though this modification seems to be insignificant at first sight, it introduces a great enhancement in the way that the class can be used. Why am I saying this? Well, as you値l realize, now it痴 feasible to call the class痴 method statically without having to create any objects from it. Simple and efficient.

    But I知 not trying to sell you this simple helper class here. Instead, I壇 like to show you how to use it now that its methods have been defined as static.

    Therefore, in the last section of this tutorial I知 going to create another example to demonstrate a proper usage of the text helper class, this time by calling its methods out of the object scope.

    To see how this final example will be developed, please click on the link below and read the following segment.



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

       

    PHP ARTICLES

    - PHP Excel Customizations: Working with Formu...
    - MVC and the Zend Framework
    - PHP: Rendering Web Pages Using the Composite...
    - PHP: Creating Dynamic Web Pages with the Com...
    - Secure Encrypting and Decrypting for Your PH...
    - PHP: Creating Single View Objects with the C...
    - PHP: View Object Collections and the Composi...
    - Securing Your PHP Website
    - Determine Link Relevance and Unique Class C ...
    - PHP Composite View Design Pattern: Introduci...
    - Facebook PHP API Applications: A Second Look
    - Secure PHP Programming
    - Facebook PHP API Applications: Basic Introdu...
    - SOAP Servers and Web Services
    - Using XPath, SOAP, and More with Web Services


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



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