03-21-2009, 10:11 PM
|
#10 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
- 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.
|
|
|
|