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

Introducing Visitor Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    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:
      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


    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
     

       

    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 4 Hosted by Hostway
    Stay green...Green IT