Page 1 of 1

Using php classes

Posted: Wed Aug 07, 2002 9:24 pm
by ruth
Hi everyone,
I am developing a php/MySQL application. The development is divided into a few phases. In order to make reuse of the code. I am planning to use php classses as many as possible. However, I have a concern: if this will slow down the application because the speed is an issue for this application.

Your suggestions or experience will be highly appreciated.

Thanks in advance.

Ruth

Posted: Wed Aug 07, 2002 11:32 pm
by protokol
In short, yes, using classes will make your application slower. How much slower is all dependent upon the way that you manipulate and access the class's member functions and variables.

Often, if you are trying to achieve something similar to a 'struct' in C, classes may not be the best way to go about doing it. Below is an example of a class which emulates a struct and after it there is an example of an associative array which emulates the same thing:

Code: Select all

// Example 1:
class Birthday {
   var $month, $day, $year;

   // struct's do not have constructors but we'll put one here anyway
   function Birthday($month, $day, $year) {
      $this->month = $month;
      $this->day = $day;
      $this->year = $year;
   }
}

// instantiate an object with your birthday as the default values
$my_birthday = new Birthday(1, 1, 2002);

Code: Select all

// Example 2:
$my_birthdayї'month'] = 1;
$my_birthdayї'day'] = 1;
$my_birthdayї'year'] = 2002;
Now say for instance that you need to store these values across a bunch of different scripts. More than likely you will be using sessions. If so, storing an object in a session variable is going to be much more noticeably slower than if you store an associative array in the session variable.

Sure, there are definitely times when you might have a bunch of functions which would only make sense to encapsulate in a class. There are methods of using classes, associative arrays, and sessions all at once. Yes, these ways can be quite fast. Just consider what values you are trying to track across multiple scripts in order to determine which data structure is best for you.

Hint: Create a class which has lots of functions, but instead of member variables, keep all variables used by the class in an associative array and store it in a $_SESSION var.

Posted: Thu Aug 08, 2002 2:24 am
by twigletmac
http://www.devnetwork.net/forums/viewtopic.php?t=1089

Not much to say really 'cept we talked about classes and speed and that in the post above, might be of use.

Mac