Wanting to make sure I’m using classes properly. The main script accepts this from the user:
1. month
2. year
3. the type of event they want to find (microsoft or linux)
For the program to find a microsoft event it has to run the regular expression: ‘(ID=)+[0-9]+’
For the program to find a linux event it has to run the regular expression: ‘(ID=)+[A-F]+’
Creating an Events class seems a logical way to organize the code. I don’t want to send the required regular expression into the class from the main script if possible. I want to tell the events class if it is a microsoft or linux event and let the class return the processed data based on variables that are pre-defined in the class for each type of event.
I have existing code written as:
class Events
{
var $month;
var $year;
var $event_identifier_expression;
public function events($month, $year)
{
$this->month = $month;
$this->year = $year;
return $this->event_identifier_expression.$month.$year;
}
}
I want to use something like multiple static helper methods in the Events class instead. If I change the existing code to the code below, will it allow me to call microsoft_events and linux_events independently from outside the class, and is this correct way to create the class? (example code for calling the static functions from outside the class is appreciated) :
class Events
{
public function __construct()
{var $month;
var $year;
var $event_identifier_expression;
}
public static function microsoft_events($month, $year)
{
$this->month = $month;
$this->year = $year;
$this->event_identifier_expression = '(ID=)+[0-9]+';
return $this->event_identifier_expression.$month.$year;
}
public static function linux_events($month, $year)
{
$this->month = $month;
$this->year = $year;
$this->event_identifier_expression = '(ID=)+[A-F]+';
return $this->event_identifier_expression.$month.$year;
}
}
I would suggest a different approach to this. Having different methods for each even types will require you to write repeating code, which will eventually lead to costly maintainable code and error prone (even copy/paste is error prone! and I’ve seen it in some projects to prove it).
First, static calls should be limited only (or mostly) when methods does not require to store data in class members; a static method should not set anything, less data to be used by instances of the class itself. You should therefore use the singleton pattern.
Second, if you should have many event types, each should have their proper specialized class. Object oriented design goes from abstract to concrete, general to specialized. Therefore, your
Eventsshould not know how many or what event it stores, but should leave that to the caller. This will enable you to have a more coherent interface.Here would be an example design :
Now that we have our container, we will need event types, first we have our base class :
Then, simply specialize it (we create two specialize event types) :
Adding new events
Get events
As you can see, the overhead for class design is a little more complex, but the API is a lot more consistent afterwards. A good design must apply the reusable pattern and this is accomplished here. Hope this helps.
******EDIT**** since your question has changed, here is an edited solution. It is much smaller, but still follow the same base design :
Then our base event type class
Now, we specialize the types
Then we test the results