The legal tricks-Learn Your Self

Latest gadgets,softwares,hardware,reviews,programming and campuses, game cheats ext......

Simple PHP Class Tutorial

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!

The two files used can be found below:
time.php
class.Time.php

Right here we go…

Brief overview…

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 get stuck in

Let’s start by creating a new file called time.php. Within this file, let’s add some code:

File: time.php

Code:
$sTime = gmdate("d-m-Y H:i:s");
print 'The time is: ' . $sTime;
?>


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 (i.e. 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!

Get in class!

Create a new file (keep it in the same directory for this tutorial). Let’s call it class.Time.php. Add the following code:

File: class.Time.php

Code:
class Time {
function GenerateCurrentTime(){
$sTime = gmdate("d-m-Y H:i:s");
return $sTime;
}
}
?>


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 (i.e. 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:

File: time.php

Code:
include ('class.Time.php');
$oTime = new Time;
$sTime = $oTime->GenerateCurrentTime();
print 'The time is: ' . $sTime;
?>


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:

Quote:
class Time {


If we had used:

Quote:
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:

Quote:
$sTime = $oTime->GenerateCurrentTime();


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:

Quote:
$oTime->GenerateCurrentTime()


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 ‘.

0 comments: