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.