PHP Classes
Written by admin on September 14th, 2008 in PHP Tutorials, Tutorials.
In PHP there are many methods of programming a script or website. Advanced programmers use something called classes. These help programmers to organize their code and also make the script more versatile. PHP classes were mainly used when PHP5 was introduced as PHP5 concentrated on advanced programmers with using classes and other methods of programming. By using classes functions can be manipulated and security can be increased from standard php methods. PHP classes or often refered to as OOP which stands for Object Orientated Programming because we are using objects to organise our code. PHP classes have many features to them and it would take to long to explain every feature of a class so were just going to look at the few basics of using and creating a PHP class.
To start, we must understand what a class is. Some may see a PHP class as a group of functions and variables working together to form a framework. You can have unlimited classes and they can all work with each other if set and referenced properly. Compared to standard or basic PHP code, classes have strict rules that must be followed to prevent errors. To start with lets see what a class looks like.
A PHP class looks like this structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php class name { function name() { PHP Code } } ?> |
As you can see the start of the class is: class name. The class statement lets PHP know that we are starting a class. Afterward is the name, and this can be anything as long as it has no spaces or special characters. To house a class we use the same method as functions. We use { } (brackets) to open and close the class that we are creating. Inside the class is where your PHP variables and functions will be put.
Before we look any further into how a class works we will go over some of the rules. Inside a PHP class there can only be two (2) types of code; Variables and Functions. Inside the functions can be anything you wish as long as it is PHP code of course. Variables inside classes must be define as a variable. This is done like the following:
1 | var $name; |
Now, those are the basics. PHP Classes are used to store a group of functions that can work together or seperately. Functions in classes are defined in the same way as normal functions like so:
1 2 3 | function myfunction() { } |
Those are the basics of PHP Classes. Now lets take a look at what a class would look like once we’ve done a bit of programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php class myclass { var $time; function myfunction() { $this->time = time(); $date = date("m-d-y", $this->time); } } ?> |
Now by looking at that logically it doesn’t make any sense to some people. Lets run through it. We have started our class called myclass. Inside it we have made a variable called $time. Now obviously we are going to use this to get the time. Next we have created our function called myFunction. Now this is where you might be confused. Inside we have:
1 | $this->time = time(); |
Lets stop. Lets talk about this part: $this->time. Okay in classes we have what are called object indentifiers. These are a way of accessing functions and variables within classes. $this refers to the current class that we are in. So by logic $this->time is simply the variable we put in our class at the beginning. So within our function we are giving our variable $this->time a value. But why did we not do that to start with?
Class variables can only be given values or types within functions. This is another rule of classes you must remember.
The next bit of our function is some simple php to get the date from our time. Then we close our function and class.
If you have understood that so far you are doing great, if not read over it again until you do understand or it is pointless continuing.
Now we have our class and our variables and functions in it, thats great but what do we do with it now? Well this is the simple bit. We are going to put our while class into a standard PHP variable. This is done with the following code:
1 | $class = new myclass; |
Okay so what we have here is, a standard PHP variable. We are putting a value to it. The statement new is making a new class identifier. the text after it is what we called our class. Now from this we can access our class and anything in it with $class. Now thats a lot to understand so read over it a few times to get it in your head solid.
So here are some examples we can with that variable.
1 2 3 4 | echo $class->time; // What is the time in UNIX seconds? $class->myFunction(); // Here we can run our function inside our class. |
So in the end we have the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php class myclass { var $time; function myfunction() { $this->time = time(); $date = date("m-d-y", $this->time); } } $class = new myclass; echo $class->time; ?> |
Now to finish up here are few pointers about what you have learnt and some rules.
If you are making a script with multiple classes, there cannot be two classes with the same name as you will not be able to reference them at all from each other.
Classes can become complex and confusing, keep it simple and comment your code so you can reference everything you do, especially with multiple classes and indentifiers.
I hope this has gave you an insight to PHP classes.
Practice and enjoy.
September 16th, 2008 at 5:31 pm
Good tutorial.
However, with the mass use of PHP 5, I believe it would be better practice to make use of Public, Private and Protected methods and variable definitions. Although that may have been outside the scope of this tutorial.
Great read though, very well explained