I’m not sure how to start this one… It can be quite difficult to understand PHP classes at first, but hopefully I’ll make everything seem easy! Let's just get stuck in shall we…
Ok so you’re actually reading this brief overview? You must be serious…
PHP classes can be used to group together a set of ‘like’ functions used within a bigger application. Their main advantage is the fact that you can edit the particular class function, or functions and make a site-wide change. Classes also help give you a more structured approach to programming, and those that like to hack with some GPL released web applications will have a much better understanding of the workings of them.
This may not be the best example of explaining why to use classes in PHP, but it’s an example of how to use them.
Let’s start by creating a new file called time.php. Within this file, let’s add some code:
This will simply assign the current date and time to the variable $sTime and then print the string 'The time is' with the variable value at the end (e.g. The time is: 09-02-2007 21:42:28)
How would we do this, using a class? Well there's many ways, however I would recommend using the class file to generate the time, then use the acutal 'action page' (time.php) to output the time. Let's create our class file!
Create a new file (keep it in the same directory for this tutorial). Let’s call it class.Time.php. Add the following code:
Lets do this line by line… The first line, class Time {,declares the class as open (exactly the same as a function in PHP, but without the brackets in this case). The next line declares a new function. The difference here is that it exists ONLY within the scope of the class (e.g. it's built WITHIN the class). We then generate the time as we did before, assigning it to the variable $sTime and then return the value of this variable. The function then closes, followed by the class closure (the squiggly brackets '}'). Note that our class needs to also be wrapped in php tags ().
Now open the original file, time.php, and change the code to match the following:
Now, the first line here includes the time class file (include ('class.Time.php');). We must include all the class files we wish to take advantage of, otherwise how the hell would PHP know about these files?
The next line, $oTime = new Time, creates the class object and stores it in the variable $oTime. Notice, to store the class in an object variable, we use VARIABLE = NEW CLASSNAME. VARIABLE can be anything, then there must be an equals sign '='. NEW must use 'new' or '&new', and the CLASSNAME must match the name of the class. In this case, the name of the class is Time (case sensitive - as PHP is throughout). The class name is 'Time' because we created the class using class Time {.
If we had used class HelloWorld {, as you can guess, the class name would be 'HelloWorld'.
Anyway… now we've created our class, we have also included it within the page we want to make use of it. Not only that, we have ALSO initalised our class by defining it in an object variable - $oTime.
So, the next line:
This simply assings the variable $sTime with the result of the function GenerateCurrentTime() within the Time class. How does it do this? Simple… We want to use the function GenerateCurrentTime() within the class $oTime so we simply us:
This tells PHP exactly what we want to do. The '->' explains to PHP that the prefix (in this case $oTime, which we know holds the class object) is the parent of the latter (again, in this case the latter is GenerateCurrentTime()). So it basically means, run GenerateCurrentTime() within the $oTime class. Thus assigning whatever is returned by the function GenerateCurrentTime() to the variable $sTime.
The last line does what we did from scratch… print out the results with the prefixed string 'The time is'.
In the next installment, we will discuss what $this-> means, and how it can be immensly beneficial to you to use classes!
As a newbie to OOP your class tutorial is very easy to follow and I am waiting for your next tutorial on $this.
Regards
Marea
Well I'm looking forward to the next php class tutorial. I came this far before but when you use several functions and error handling in 1 class, it becomes a nasty trick
I know little php. You tutorial on php OOP help me very much. Keep in touch. Thank you vary much.
I have been using Java for some time now and was wondering how PHP class or object relate to Java.
Does it follow the same OOP approach that is used in other languages?
Thanks for the tutorial, it help me a lot.
GenerateCurrentTime();
print 'The time is: ' . $sTime;
//or just this
print 'The time is: ' . gmdate("d-m-Y H:i:s");
?>
I have found your tutorial so simple to follow that in the last couple of weeks, I've been working my way through a copy of PHP book I got at the local library.
Thanks for the tutorial
Thanks very much for the valuable TUT.. Still rocking TUT are available
Thank you for breaking down just the basics on php classes. This is exactly what I needed.