PHP Class Tutorial | edrackham




Easy to follow PHP class tutorial (which is Object Orientated Programming – OOP). 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…

Update! Shameless, shameless plug…

I’ve made a free iPhone app (What Should I Make For Dinner Tonight?) and was wondering if you would be interested in the source / helping me to improve this app.

PHP Class Tutorial Chapters

Part 1 – Jumping In With Two Feet
Part 2 – What does $this-> mean in a PHP class file?
Part 3 – What Are Class Constructors?

Brief overview…

Ok so you’re actually reading this php class tutorial overview? You must be serious about learning how to create and use PHP classes…

Firstly, when you hear someone talking about PHP with Object Orientated Programming, or OOP as it’s otherwise known, they are in-fact referring to PHP classes. 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 (with PHP classes) 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.

To begin our PHP class tutorial, let’s start by creating a new file called time.php. Within this file, let’s add some simple code (this isn’t a PHP class yet!):

$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 (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!

Get in class!

So, now we’re going to make the above into a function, which will be placed inside our new PHP class file, for future use. Create a new file (keep it in the same directory for this tutorial). Let’s call it class.Time.php. Add the following 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). This tells PHP that we have a new class, and we’re calling it ‘Time’.

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 PHP 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 ‘}’, or “close-stache”). Note that our class needs to also be wrapped in PHP tags (<?php … ?>).

Now open the original file, time.php, and change the code to match the following:

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

Now, the first line here includes the time PHP class file (include (‘class.Time.php’);). We must include all the PHP 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 PHP class name is ‘Time’ because we created the class using class Time {.

If we had used class HelloWorld {, as you can guess, the PHP 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. Now, it’s not completely covered within in the scope of this PHP class tutorial, but you can kind-of think of $oTime being a variable which stores functions that we can do many things with.

So, the next line:

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

$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’.

In the next PHP class tutorial installment, we will discuss what $this-> means, and how it can be immensely beneficial to you to use PHP classes!

PHP Class Tutorial Chapters

Part 1 – Jumping In With Two Feet
Part 2 – What does $this-> mean in a PHP class file?
Part 3 – What Are Class Constructors?


Categories: PHP


28 Responses so far.


  1. Marea says:

    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

  2. Robin says:

    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 ;)

  3. Arifur Rahman says:

    I know little php. You tutorial on php OOP help me very much. Keep in touch. Thank you vary much.

  4. Helen Hunt says:

    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?

  5. Noel Pulis says:

    Thanks for the tutorial, it help me a lot.

  6. Still why says:

    GenerateCurrentTime();
    print ‘The time is: ‘ . $sTime;

    //or just this
    print ‘The time is: ‘ . gmdate(“d-m-Y H:i:s”);

    ?>

  7. Helen Hunt says:

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

  8. Peter says:

    Thanks very much for the valuable TUT.. Still rocking TUT are available ;)

  9. Brandon says:

    Thank you for breaking down just the basics on php classes. This is exactly what I needed.

  10. Awais Anis says:

    good basic example of class is provided!!!

  11. desis says:

    I am new in OOP .. this is very nice one to follow

  12. [...] make sure you’ve followed my first PHP Class Tutorial before starting this one, as it follows on using the previous example. This tutorial will explain [...]

  13. [...] Part 1 – Jumping In With Two Feet Part 2 – What is $this-> Part 3 – What Are Class Constructors? [...]

  14. Erin says:

    This tutorial is fantastic for someone just getting into OOP in PHP. Thank you so much for the lesson!

  15. hygsan says:

    thanks for the lesson :)
    now i know, i should learn OOP!!!
    i’ll never “re-invent the wheel” again!!! =))

  16. Frank says:

    This is excellent!
    Thank you very much!

  17. dr4g0n says:

    wonderful…

  18. Thank you for this Tutorial. I like the easy way in which you explain everything.

  19. jozaf says:

    really a good tutorial for beginner

  20. jozaf says:

    really a good tutorial for beginner guinness

  21. ronald says:

    thanks for the tutorial. very easy to follow.

  22. It is a perfect turorial for beginers like me.

  23. Thanks for your many years of a great service well done! I’ve always felt good
    about listing my concerts with you and linking from my website.

  24. gtwebworx says:

    very helpful to all newbies in php oop…keep posting related tutorials coz I’m following. :-)

  25. Thanks alot, very good tutorial

  26. csColeman says:

    Bravo! It was time to review with a much better grasp on using classes, and your explanations and examples have made this tutorial just the thing that I was looking for. Thanks again, keep up the good work!

  27. Raihan Taher says:

    Thanks for such a nice post. My fear of OOP is now going away.. But is it necessary to name a class file,

    class.Time.php

    or it can be just

    Time.php

    Thanks

Leave a Reply