tags:

views:

167

answers:

4

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;
      }
    }
+3  A: 

$this can't be used within a static method - one can't assume that the class will be instantiated here...

You would have to declare your class like this:

class Events
    {
        public static $month;
        public static $year;

        public function __construct(){    }

        public static function microsoft_events($month, $year)
        {
            self::month = $month;
            self::year = $year;
        }

        public static function linux_events($month, $year)
        {
            self::month = $month;
            self::year = $year;

            //do something?

            if(self::month == 'March' && self::year == '2010'){
                return 'Linux horoscope looks great';
            }
            return self::month . ' ' . self::year . ' is even better!';
        }
}

Calling:

$returnedVar = Events::linux_events('March', '2010');
Michael Robinson
+4  A: 

If you want to use everything statically (which makes it essentially a prettied up procedural piece of code), you'll have to do something like this:

class Events
{
   static protected $month;
   static protected $year;

   public static function microsoft_events($month, $year)
   {
      self::$month = $month;
      self::$year = $year;
   }
   public static function linux_events($month, $year)
   {
      self::$month = $month;
      self::$year = $year;
   }
}

Events::microsoft_events(12, 2010);

Note that you can only have a single instance then, every call will change the values for everything.

Blizz
My goal is to make a class that uses the same variables for each static function. Each static function however will provide a different set of static values to the class variables. Is this approach a reasonable solution to my goal?
JMC
Euh no it doesn't make sense actually... You either use it statically or you use instances :) If you want to be able to use the same class with different variables each time, you have to use instances :)
Blizz
+3  A: 

Off the top of my head, i can suggest something like

/// Raw event data, e.g. database
class EventStore { 
    function getRawDataMatching($pattern) {
       // query the db
    }
 }

/// Mother of all Event types
abstract class Event {
    static function getSearchPattern() {
       // return the search pattern for the concrete event class
    }
    function __construct($rawData) {
       // initialize from the raw data
    }
}

/// Concrete event classes
class MicrosoftEvent extends Event {...}
class LinuxEvent extends Event {...}

/// Find events in the stream
class EventFinder 
{
    function __construct(EventStore $store) { ... }

    function classFor($type) {
      // return event class name for the given type
    }

    function find($date, $type) {
       $klass = $this->classFor($type);
       $pattern = $klass::getSearchPattern();
       foreach($this->store->getRawDataMatching($pattern) as $data)
            $result[] = new $klass($data)

    }
}

// main code
$store = new EventStore($connectInfo);
$finder = new EventFinder($store);
$foundEvents = $finder->find($input->date, $input->type);

This is one of many possible ways, of course. Let me know if you need further comments.

stereofrog
+1 - This is the kind of code I was trying to conceptualize going into the problem. There are so many options for mixing classes it gets confusing fast. Extending Event and using an abstract class makes perfect sense. Thanks
JMC
+1  A: 
Yanick Rochon
The second solution after the edit works well. Have not tried the first solution yet. The level of detail in the code helped fill in the gaps. Thanks
JMC
Well, the first solution was what I thought you wanted before you edited/modified/detailed your question. That sample code is only useful if you want a simple "EventStore", as stereofrog have proposed (his solution is also good by the way). Usually, you will use a database to store your events and thus query the Db, and therefore would modify the getEvents() and addEvent() functions to query your event table. Tell me if you need more information about it.
Yanick Rochon