PHP
  Home arrow PHP arrow Page 3 - Introducing Visitor Objects in PHP 5
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

Introducing Visitor Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-08-02

    Table of Contents:
  • Introducing Visitor Objects in PHP 5
  • An introductory example
  • Defining another sample class
  • Defining the structure of a visitor
  • A visitor object in action

  • 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


    Introducing Visitor Objects in PHP 5 - Defining another sample class


    (Page 3 of 5 )

    As I expressed earlier, I want to expand the practicality of the previous example by coding another sample class, which also accepts a visitor object. In this case, this new class is called "VisitedFile," and its signature is as follows:

    // extend 'VisitedFile' class from abstract 'VisitedData' class
    class VisitedFile extends VisitedData{
        private $filePath;
        public function __construct
    ($filePath='default_path/data.txt'){
            $this->filePath=$filePath;
        }
        // add new line to data file
        public function addLine($line){
            if(!$fp=fopen($this->filePath,'a+')){
                throw new Exception('Error opening data file');
            }
            fwrite($fp,$line."n");
            fclose($fp);
        }
        // get file size
        public function getSize(){
            if(!$size=filesize($this->filePath)){
                throw new Exception('Error determining size of data
    file');
            }
            return $size;
        }
        // get number of file lines
        public function getNumLines(){
            if(!$numLines=count(file($this->filePath))){
                throw new Exception('Error determining number of
    lines for data file');
            }
            return $numLines;
        }
        // accepts 'visitator' object and call 'visitFile()' method
        public function acceptVisitator(Visitator $visitator){
            $visitator->visitFile($this);
        }
    }

    As shown above, the functionality of the new "VisitedFile" class is only limited to saving plain strings to a given text file. However, my intention here is that you pay attention to the respective "acceptVisitator()" method, since it's closely similar to the previous one that you learned in the past section.

    Also, on this occasion, the visitor will call its visiting method, and pass to it the visited object, with the purpose of getting additional information about it. As I said before, this model of object interaction is practically identical for all the cases where the visitor pattern is implemented. I hope you'll have no problem grasping this concept.

    Okay, at this point you learned how to create two subclasses that accept visitor objects, but... how can these visitors be created? Do you have any clue about that? Well, to dissipate any possible questions, over the course of the next section, I'll show you how to define the structure of visitor objects.

    To learn how this will be done, click on the link below and continue reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In this first installment of the series, you'll learn how to apply the visitor...
       · Alejandro,Thank you for writing such high quality material. I can't remember...
       · Hi Larry,Well, I guess this will be rather redundant but I thank you for all the...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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