View Single Post
Old 03-22-2009, 02:15 AM   #11 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
  • Create custom class called ApacheFilter that extends from existing class FilterIterator from PHP's built-in SPL
    Absolutely correct.

  • This is a protected property only available to the ApacheFilter class methods
    Whilst this is true, it's also important to note that any classes extending from ApacheFilter will be able to define and modify this property. Compare that to declaring it as 'private'.

  • This is the class's main construct. It accepts 2 attributes. Type casting the "$it" with Iterator?
    It does not cast the $it argument at all. The term to use is 'type hinting' and declares that this argument should implement the Iterator interface. Failing to pass an iterator as the argument will generate a "Catchable fatal error" stating that "Argument 1 passed to ApacheFilter::__construct() must implement interface Iterator".

  • You are passing the "$it" attribute to the static parent???
    This is calling the parent class's constructor. In the case that we define our own behaviour with a custom __construct method, we need to manually call the parent's constructor if we want to use it. Essentially it's calling FilterIterator's __construct method. More info.

  • You are passing this attribute to the class pointer "filter". Type casted as array?
    Yes, the value is type casted to an array (for example, if only a string is provided it will be converted to an array containing that string). The array is assigned to the filter property of the class for use later.

  • Create a method accessible outside of the class
    Yes.

  • NEED HELP UNDERSTANDING THIS
    This is some of the OOP magic inherited from the FilterIterator and beyond. Remember the iterator that we passed along when creating the instance of our class? Well, the getInnerIterator method retrieves that iterator (our SplFileObject) for us. We then call the current method on that iterator (our SplFileObject) to retrieve the current line of the file. The terminology used for stringing multiple method calls together is 'method chaining' which is an incredibly useful (and easy to misuse!) feature introduced for us in PHP5 (not sure if you've seen or used chaining before).

  • substr function. Take the first part of the line that is before the space '' Start at the beginning of the line and up to the space ''
    Yes. In these Apache logs, the IP address is always at the start of the line and followed by a space character.

  • If TRUE, return the line that has the IP address found in the array
    This line returns TRUE if the IP address matches one in the filter array, FALSE otherwise: this is the purpose of the accept method (see below). The type casting to bool is only there so that I (or you, or anyone else reading it) can quickly see that the function returns a boolean TRUE/FALSE (which in_array does by default) and isn't strictly necessary.

  • This is the object we create to access the file we want to read
    Yep. SplFileObject is a utility class which can read and write files, and access useful information about them, in a variety of ways. More info.

  • This is the object we create to access the ApacheFilter class and its methods. We pass the newly created file object and array data
    Yes.

  • This is just a simple array loop to echo out our data
    Yes. Note that since we're iterating over the filter, only our filtered lines get echoed. This is where the accept method comes in to play. The accept method is automatically called as the foreach loop goes through each line. It defines which lines should be accepted or not (by returning TRUE/FALSE respectively) and is therefore where we place our filtering code.

Wow I cannot thank you enough. The Thank You button only let's me click on it once...

I've read about 3-4 books on OOP the last 2 months. My favorite is this one: The Object-Oriented Thought Process by Matt Wiesfeld.

Maybe you've read it or heard of it, but I like that it's not PHP related but just the overall thought process of OO design.

Anyways, this OO stuff is taking a long time for it to sink in for me. Especially things like method overloading, and the many types of design patterns.

Thanks again and I am still going to look at your code so I fully understand it.

In all the books I've read, I don't recall reading up on Method chaining so I'll have to do some digging on that, sounds pretty important!!! (unless there's another term out there for method chaining?)
allworknoplay is offline  
Reply With Quote