jurriemcflurrie wrote:I've made several attempts to code with classes with php and I'm getting more used to it day by day. But as I code along I still can't find out the usefulness of it. I can't get an answer to it neither.
Why can't I just put several functions in a seperate file? Thats what you do with classes also, right?
I dunno if i'm missing something but I'm really missing the point of classes. Can someone tell me why I should continue to learn it. Else I'll just keep coding old style.
Thanks in advance
The best way to think about Classes would be as self contained code modules.
Here is a code example (yes very simple but effective example):
Code: Select all
class test{
var $stuff = 0;
function display()
{
echo $this->stuff;
}
}
$obj1 = new test;
$obj2 = new test;
$obj1->stuff = "This One";
$obj2->stuff = 12134;
echo "1: ";
$obj1->display();
echo "<br>2: ";
$obj2->display();
With the above example you have two instances of the same class but the data stored in each instance is different. You can call
$obj1->display() over and over and receive the same data every time, even if you call
$obj2->display() at anytime. You will also notice that all variables in a class can be used by every function in said class. The use of the
global tag isn't needed inside the various functions like they are needed for functions outside classes.
Code: Select all
function display()
{
global $stuff;
echo $stuff;
}
If the display function wasn't in a class you would have to populate the $stuff variable from another variable every time you want to execute the display function.
Code: Select all
$stuff = $stored1;
display();
echo "<br>2: ";
$stuff = $stored2;
display();
Using classes can make managing your variable structures alot easier in some instances. With that said classes are not the end all for PHP. Classes and Procedural each have there own uses depending upon the situation and one isn't necessarily better than the other. In many cases using precedural programming methods are faster than using classes. One of the problems with classes in PHP is memory usage and speed. Classes in PHP can create a large overhead in memory if the programmer isn't careful in how they impliment the usage of classes. Coupled with this is how classes can slow down the number of pages per second that can be processed by the server. Now I am not talking in how fast the functions inside the classes can execute but how fast an object is created for a class. The size of the class, number of extentions and number of classes all affect how many pages per second can be processed. With PHP 4 and even with PHP 5 a class heavy web page is going to be heavily impacted where a similar web page that minimally uses classes will process more pages per second.
For PHP the use of classes is fantastic for creating self-contained modules that can easily be transported between different projects. It reduces the amount of duplication and rewriting of code. But creating an entire web site using only classes will end up being more of a load on the server than one that uses procedural programming or a combination of procedural and classes for most of the site.
Bottom line with PHP is how the usage of classes and procedural program go hand-in-hand together. They both assist each other and you should use both methods if you want the most out of PHP.