LearnGetting Started with OOP & PHP5

writes on March 26, 2010

Share with your friends


Since the introduction of PHP 5 in 2004, PHP has had an object model worthy of that description and became a truly modern language for use on the web. Earlier PHP scripts would have been of the kind where, to quote from Alice’s Adventures, you would “Begin at the beginning, and go on till you come to the end: then stop.”

Nowadays that very procedural approach is less common in PHP, so this article takes a look at some of the basic object oriented features available in the language and shows some examples of using them with code examples.

Using OOP (Object Orientated Programming) enables us to architect our systems much more clearly, and to make them more manageable and more maintainable. This technique also allows us to separate form from function to create clean, navigable codebases with plenty of opportunities to reuse code, apply design patterns and bring in concepts from other brances of computer science.

Objects vs Classes

While the terms “object” and “class” are often used almost interchangeably in the world of software, there is a definite conceptual difference between the two. A class is the blueprint or recipe; it describes what the object should be, have and do. So a class might look like this:

class Elephpant {
    public $colour;

    public function dance() {
        echo "elephpant dances!n";
        return true;


An object, on the other hand, is an actual instantiation of the class – its an actual thing, with values and behaviours. In the example below, $ele is the object:


$ele = new Elephpant();

We can inspect $ele to make sure that it is, in fact, an object of the class Elephpant, using the print_r command. Adding this to our code sample as shown, we see the output below:


$ele = new Elephpant();

Elephpant Object
    [colour] =>

This output shows an object, of type Elephpant, and with a single empty property named “colour”. For more information about the objects exact properties and their values, you can also use var_dump, which gives a more detailed output showing in detail the datatypes of each property. This is particularly useful for spotting empty strings, nulls and false.

Using Objects, Their Properties and Methods

Our class, Elephpant, has a property and a method (OO-speak for “function”) already defined inside it, so how can we interact with these? Let’s start by setting the colour property; we use the object operator which is a hyphen followed by a greater than sign.


$ele = new Elephpant();

// set the colour property
$ele->colour = "blue";

// now use that property
echo "The elephpant is " . $ele->colour;

The output of this script reads “The elephpant is blue”. The property will remain set on the object until the object is destroyed, or the property overwritten or unset. Objects are a great way of keeping things together that belong together, for example information about an elephpant!

Similarly we can call the method using the same operator – the brackets after the call let PHP know its a method rather than a property, and if we had parameters to pass in they’d go between the brackets. Something like this:


$ele = new Elephpant();

// call the dance method

Look back at the class declaration for Elephpant and you’ll see the dance method actually echoes from within it. Indeed when we run the code example here, we see “elephpant dances!” as our output. It is a very trivial example but I hope it does show how to call methods against our objects. An alternative approach (and probably a better one within an actual application) would be to create the string and use it as the return value for the function. Then the calling code can take the string and echo it, or do whatever else it needs to, in a more flexible way.


Now we know how to create and interact with objects, let’s step things up a bit and look at how we can create objects which are similar in some ways and different in others, using inheritance. If you are accustomed to OOP from any other programming languages then this will seem very familiar to you, so here is a quick look at how this can be done in PHP. We already have our Elephpant class declared, so let’s add a Penguin class as well. They will both inherit from the parent class Animal, which looks like this:

class Animal{

    public $type = "animal";

    public function dance() {
        echo $this->type . " dances!n";
        return true;


The animal has a “type” property, and a dance() method. It uses the type property in the dance() method to create the output, using $this to refer to the current object. In PHP, $this is a special keyword which refers to the current object from within its own class.

Now we can create a Penguin class that inherits from this general Animal class, by using the extends keyword to denote its parent:

class Penguin extends Animal {
    public $type = "penguin";


The penguin class would inherit the type property set to “animal” if we didn’t override it by setting the property again in this class. However even without declaring a dance() method, the penguin can dance!


$tux = new Penguin();

// make tux dance

The above example gives the output “penguin dances!” – using the dance() method from the Animal class, which is available because the Penguin class extends it, and the type property set separately in the Penguin class itself.

Access Modifiers

Take a look at the previous example. The type is set in the class, but we could easily set it from our main code if we wanted to. Imagine if we put $tux->type = “giraffe” before we asked him to dance! Sometimes this is the desired behaviour, and sometimes it isn’t. To allow us to control whether properties can be changed outside a class, PHP gives us access modifiers. An example of these are the “public” keywords you see in the classes shown above. To prevent the type property being edited from outside the class, we can declare the Penguin type to be private, so the class now looks like this:

class Penguin extends Animal {
    private $type = "penguin";


The following code listing shows us trying to set the now-private type property, and is followed by the resulting output.


$tux = new Penguin();

// change the type
$tux->type = "linux penguin";
// make tux dance

Fatal error: Access level to Penguin::$type must be public (as in class Animal) in /home/lorna/.../OOP/private_penguin.php on line 5

The resulting message feeds back to the user that the type property can’t be modified, and even includes the detail that the property was public in the parent class. Access modifiers are pretty powerful, we should look at them in more detail.

Access modifiers can be applied to properties and to methods and to properties. The options are public, private and protected. In PHP4, these weren’t available and everything is public. As a result, and to maintain backwards compatibility, if an access modifier isn’t specified then the property or method defaults to being public. This isn’t recommended practice however, and it is best to be explicit about which is intended.

Public: The public access modifier means that properties and methods can be accessed from anywhere, within the scope of the object itself, and also from outside code operating on an object.

Private: The method or property is only available from within the scope of this specific class. Before using this option, read on to find out about the “protected” access modifier.

Protected: The method or property is available from within this class, and from within any classes with extend or implement this class. This is ideal where you don’t want external code to change the class, but you do want to be able to extend the class later and take advantage of this property or method. Protected is more flexible than private and almost always the better choice.

Using these access modifiers we can control where our class methods and properties can be accessed from. We looked at an example of properties and this works in the same way for method calls – they cannot be accessed from outside of the class definition unless they are declared to be public. It can be very useful indeed to declare methods as protected, where they are internal helper methods used by other class methods but not intended to be accessed directly. In PHP4 there was no support for this and so the internal methods were sometimes named with an underscore to hint that this was their intended use – it is still possible to see this naming convention in use today, although it is not needed.

Using OOP in PHP: Practical Example

Object Oriented design is particularly useful where you have data objects that relate to one another – so it is very common for example to have OOP used to deal with data. PHP 5 has the PDO (PHP Data Object) classes which are a great way to talk to a backend database, either mysql or any other kind of database, and using the object oriented interface offered by this extension ties in well with the usage I have shown here.

A common mistake for those coming to OO for the first time is to declare methods inside a class but then instantiate a single copy of that and pass in data such as the ID of the data to each method. Here’s an example of a user class that does this:

class User {
     * getDisplayName
     * @param int $user_id the user_id of the user in the database table
     * @access public
     * @return string Display name of this user
    public function getDisplayName($user_id) {
        $sql = 'select display_name from users where user_id = '.$user_id;
        $results = mysql_query($sql);
        $row = mysql_fetch_array($results);
        return $row['display_name'];

     * getFriends
     * @param int $user_id the user_id of the user in the database table
     * @access public
     * @return array An array of friend_ids
    public function getFriends($user_id) {
        $sql = 'select friend_id from user_friends where user_id = '.$user_id;
        $results = mysql_query($sql);
        $friends = array();
        while($row = mysql_fetch_array($results)) {
            $friends[] = $row['friend_id'];
        return $friends;


And some code that uses it:


$user = new User();
echo "User known as: " . $user->getDisplayName(1) . "n";

$friends = $user->getFriends(1);
echo "Friends with: " . implode(', ',$friends) . "n";

If you want to run this code yourself, then you can set up the database tables using this script:

CREATE TABLE `user_friends` (
  `user_friend_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `friend_id` int(11) NOT NULL,
  PRIMARY KEY (`user_friend_id`)
INSERT INTO `user_friends` VALUES (1,1,3),(2,1,5);

CREATE TABLE `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(20) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `display_name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
INSERT INTO `users` VALUES (1,'Lorna','Mitchell','lornajane');


This is more like a function library than an actual object, and although it works perfectly well and does have its applications, it can be better to aim to take advantage of more object oriented features. To do this, a new object should be created for each item the system handles, and if it has an ID and perhaps some other properties, these should be set as part of the object. Then rather than creating a single user object and doing getFriends(42) on it, you’d have a user with ID 42 which did $user->getFriends().

Here’s a follow up example with a class and how to use it, this will give better performance and enable you to clearly see which data goes with which object where there are multiple items on a page:

class User {
     * getUserById
     * @param int $user_id the id of the user row in the table
     * @access public
     * @return true if the user was found
    public function getUserById($user_id) {
        $sql = 'select first_name, last_name, display_name from users where user_id = ' . $user_id;
        $results = mysql_query($sql);
        $row = mysql_fetch_array($results);
        $this->user_id = $user_id;
        $this->first_name = $row['first_name'];
        $this->last_name = $row['last_name'];
        $this->display_name = $row['display_name'];

        // in real life, there would be escaping and error handling and we'd only return true if we got data
        return true;

     * getDisplayName
     * @access public
     * @return string Display name of this user
    public function getDisplayName() {
        return $this->display_name;

     * getFriends
     * @access public
     * @return array An array of friend_ids
    public function getFriends() {
        $sql = 'select friend_id from user_friends where user_id = '.$this->user_id;
        $results = mysql_query($sql);
        $friends = array();
        while($row = mysql_fetch_array($results)) {
            $friends[] = $row['friend_id'];
        return $friends;



$user = new User();
// populate the object
echo "User known as: " . $user->getDisplayName() . "n";

$friends = $user->getFriends();
echo "Friends with: " . implode(', ',$friends) . "n";

In Summary

This has been a very introductory look at some of the object oriented features available in PHP, to get you started out with the basics of working with OO code and also writing your own. If this has helped you take your first OOP steps, then let us know by leaving a comment and let us know what you built! In the next post there will be some more in-depth content including using constructors, access modifiers and working with the static keyword in PHP – taking your skills to the next level.

54 Responses to “Getting Started with OOP & PHP5”

  1. srinu on May 14, 2013 at 7:39 am said:

    very use full programe

  2. Thank you for the example, but s/Elephpant/Elephant/g

  3. Rajssharma81 on March 15, 2011 at 9:41 am said:

    This is the site is very good. thanks lot
    easy way you can solve your OOPS Concept

  4. Ele *php* ant


    Get it?

  5. Sudhir_satvik on December 30, 2010 at 5:17 am said:

    Thank you for the great read. I noticed that “Elephpant” is incorrectly spelled

  6. AgilityGap on December 24, 2010 at 9:35 pm said:

    Thanks much…very nice explanation. Love the blue Elephpant!

  7. Thank you for this great introduction. Really good information to learn OOP for a newbee. Thank you very much! πŸ™‚

  8. Thank you for the great read. I noticed that “Elephpant” is incorrectly spelled.

  9. cawanpink on July 19, 2010 at 12:19 pm said:

    i’ve found this very useful! using it as part of the content for a cakephp beginner class here in malaysia.

    thanks alot!

  10. I was looking for a beginner tutorial on OO in PHP to put up as recommended resource on WaSP Interact’s Server Side Scripting course. This one came close, since it is clear, effectively narrated and has some nice illustrations, but it also has a few nagging bits missing.

    1. For any new project, one should not use the outdated mysql-functions, but mysqli or PDO. Students will use every example they are shown, even the bits that are out of scope for the actual lesson.

    2. Which means security can NEVER be outside the topic. If a code does not include appropriate checks and escaping, please say so clearly and attach appropriate warnings. (BTW, preg_match to ensure integers is not really the best option. ctype_digit() or a simple typecast with (int) would be a more effective choice.)

    Yes, I notice the comment about escaping and error handling but that does not seem to apply to the SQL parameter, but the returned data. Also, one of the points of OO is re-usability. If there is no SQL-injection prevention in the class-method, the class’ re-usability is diminished severely. Not having it there also breaks another important security principle, namely “defence in depth” and the consistent application of secure patterns. The latter is especially important, just because this is a beginner tutorial.

    3. (Combining 1-2) Using PDO or mysqli, one should always default to parametrized prepared statements, since that is the safest option for newbies. I teach web development to newbies for a living. Defaulting to prepared statements in my teaching has made my life as a teacher a lot simpler! The concept is not that hard for students to grasp.

    This is something that one just can apply to the examples just like you are using, but not explaining, PHPDoc comments. That is one of the features of this article I like!

    I use PHPDoc in every example I give my students, from day one. Even before I explain them (other than “just do it”) to make it a habit. Then, when I do introduce PHPDoc and they run the command to produce the actual documentation, I get a lot of smiles, since everything they’ve done so far (in this regard) suddenly makes sense.

  11. “Objects are a great way of keeping things together that belong together, for example information about an elephpant!”

    This really hit home for me, this was the missing link in my OOP idea idea of programming. I must admit I have avoided OOP with PHP for a long time due to me simply not getting why people would use it when there are perfectly good functions. Now with this it makes much more sense and I will happily code my DB queries etc using classes.

    Thanks for this and I am looking forward to moving onto the next in the series.

    BTW, it was great that you did an abstract example and then a real working one. Very useful and something I will put into our course content.



  12. Hey, I saw part II of this post in my feed, but it when I clicked the link it was showing a 404 page. A few hours later it was pulled from the feed. Any news when part II will be live?


  13. @Dylan: “Writing semantic OOP code is faster and more efficient over procedural code always.” I disagree with this common sentiment used by OOP only advocates. Procedural code is always faster to write because it’s always less verbose. That being said, efficiency depends more on the design and architecture of whatever you’re building and programmer ability. Some problems are naturally suited for OOP, others are naturally suited for Procedural or Functional programming. OOP is not better than any other style of programming, and in many cases it’s actually the wrong solution (believe it or not, OOP has really been oversold). The weird thing however, is that I’m mostly a Ruby programmer which is a completely OO programming language. Go figure.

    Also regarding your criticism on the “asking a user to get a user” thing, I would agree if it wasn’t for the fact that this tutorial didn’t cover constructors. It should have been $var = new User($id), but since that was to be covered in the next part, doing so in the manner she did is a good way to start a primer.

  14. A really good primer / intro to OOP within PHP5 – I think there is a big need for more clear explanations of the basics, It really helped . I’m sure this will go a long way to help a lot of php programmers getting their heads around POOP (PHP Object Oriented Programming) —- i’m sure it came from the blue elephant !

  15. Thank you. Always wanted to understand what all those -> arrows do and where they come from. Tried a lot of tutorials and always gave up in half of reading. Your explanation is the first I have read to the end. πŸ™‚ I see second part is ready, so I am going to continue. “Constructors” sounds scary but I hope you’ll tell me “don’t panic” again, like in this article. πŸ™‚ You should write a book for PHP beginners.

  16. Great post! I used to love using OOP in PHP,
    I actually started with PHPas a beginner some time in the past, and now you can see what I have grown into)))) Big Company!
    I can also suggest newbies to start from HTML first)

  17. Great post! I love using OOP in PHP, and am trying to figure out how to teach some others about it. This post will do great!

    Thanks for the post!

  18. Thank your for this article, it’s a great introduction to OOP.

    You are using this term throughout your article, but what is an elephpant? I guess you meant an elephant? πŸ˜‰

  19. I did enjoy the approach, and have used it to explain simple inheritance many times.
    Just a couple of things I’d like to add… use it, don’t use it.

    1) There is NO MVC pattern mentioned in this article. So stop saying it πŸ˜›

    2) Writing semantic OOP code is faster and more efficient over procedural code always. There is no argument about it.

    I’m no guru. But I feel there is a flaw in a section of this article…
    Anyone can grasp OO, but the real tricky part is actually doing it correctly.

    “Asking” a user to get a user by ID is wrong ( User -> getUserByID ).
    For this example I would have suggested moving that code outside of the user, similar to how you did the DB connection.
    Ideally it should be handled within some kind of manager (UserManager).

    Otherwise, nice and simple πŸ™‚

  20. Great start for OOP.

  21. I loved this. I am planning to post an article on OOP’s with PHP ASAP. πŸ™‚

  22. Thanks to everyone for taking the time to read and comment on this post! A couple of you have asked good questions so I’d like to pick up on those points individually:

    @dug: instantiating a single class instance and then using its functions is a mistake because it misses the point of OOP. Sometimes the objects shouldn’t be instantiated (look out for static methods and classes in the next post) but most of the time, if you have a user class, then you want to instantiate one copy of the object for each user you want to work with in your code. This means that the user object is aware of how to operate on itself, rather than you passing an ID in, for example.

    @Stephen: you are exactly correct πŸ™‚ Object properties are public unless declared otherwise – and in PHP of course we don’t need to declare them in advance at all if we don’t want to. So the display_name property can indeed be modified from external code – sometimes this is useful, and sometimes not – so think carefully about access modifiers.

    Great to “meet” you all – the next post in the series comes out later this week so look out for that!

  23. OOP and MVC rocking! That’s the only right way. Thanks for great intro.

  24. Great post. Would love if have some design pattern stuffs.

  25. Great intro into OOP

    Would the $this->display_name variable inherit the public scope of its parent function because it wasn’t explicitly declared?

    and can it be changed outside like
    $user->display_name = “Default”;

  26. You have done a lot of work. And now I know what an objekt and a class is. Thx the elefant :-). Very good.

  27. Thanks a lot.
    OOP and MVC are always confusing.
    Need more articles on this topics.

  28. Great read, I’ve just decided to learn PHP (it was between that and Ruby/Rails, but there’s still not very much work for Ruby developers where I am) and this is a good intro, cute pictures too :).

  29. Rob Charlwood on March 27, 2010 at 11:43 am said:


    I hear what you are saying here Jim and you do make a couple of good points πŸ™‚

    However, in my experience there is no such thing as a project that never gets expanded on, I find they always get expanded on either by scope creep or a clients requirements changing over the course of a few years.

    I have the unfortunate job of working with a lot of old legacy PHP websites in my everyday work alongside all our new development in Python. I can safely say, without a shadow of a doubt that I prefer working with legacy Python projects as apposed to the custom PHP CMS of procedural spaghetti code that my predecessor’s predescessor wrote!

    I agree it was definately quicker to write these sites in procedural PHP. They most probably got the code out of the door a lot quicker than our newer Python counterparts, but in the long term they have lost us more money than we make from them!

    This is because adding functionaliy takes three times as long as it would in Python, Everytime a new developer starts or we need a freelancer they have to take time to learn our custom cms mess, you get deprecated and duplicated code all over the place and stuff breaks…. a lot!!

    I am also a very firm supporter of open source web development and actually think thats its morally wrong to write something for a client that is so bespoke and custom that no other company could take over from where you left off, if for whatever reason you were to part company with each other.

    But hey this just my humble opinion, what does everyone else think?

    • Hi Rob, two things:
      1) On the though of expanding, one thing is to expand an application to do something new, while another might be to add a feature that’s inherited by the idea itself. OOP is a lifesaver on the former, and not really necessary on the later. In your case I think it would be an awesome opportunity to lead the re-factoring of your application.
      2) The spaghetti code is most probably an inherent result of old php convention (although I’m not a fan of PHP now, I do agree that it is better by a gazillion orders of magnitude now that it ever was before) and a crappy programmer, NOT because of procedural programming.

  30. Real world example: Client wants script functionality done on time. Mr. OOP took his good ol’ time and established a bulky OOP framework approach and did not get the script done on time. Client hired another guy to just get the script done.

    It all depends on time. Procedural approach works great if the project is small and does not need to be expanded on in the future. Classes are nice to use when you plan to reuse or share code with others and want to create a simple interface for the programmers who are going to be using your code. This encapsulation helps hide the complex code from the novice programmer. Personally, I like to create my own framework and not be hindered by someone’s idea of the best way to do something. I have popped many wolf egos. Think outside the box and do not drink to much of the same old.

  31. I find OOP very confusing. This article helped me get a little closer to not finding it so confusing. Thank you!

  32. Thank you for this article! This was an awesome read. Very clear introduction to OOP… (and that’s a hard thing to do!)

  33. 5 years ago… i was hoping to find sumthing as clear as this to learn how to manipulate and use objects in my php projects… this is just very useful for young programmers, i wish i would read it on my early years..

    Nice Article! keep it up!

  34. Good job on pointing out the difference between OOP and function libraries. Many ad hoc OOP applications out there are just poorly coded function libraries in disguise.

  35. This is an excellent introduction to OOP with PHP. I find that a lot of authors, with best of intentions, skip some things that aren’t obvious to the complete newbie. Yours is an exception to that case, so well done!

  36. Rob Charlwood on March 26, 2010 at 6:18 pm said:

    Hi Lorna,

    This is a good introduction for beginners! πŸ™‚

    I have been working professionally with PHP for about 5 years (8 years total) and up until recently I loved it and used PHP5 whenever I could.

    However about a year ago I got a job working with the Python and Ruby and I have barely touched PHP since! I personally found that learning OOP using PHP was a lot more difficult than learning the same techniques and theories with a native OOP language. I don’t feel that PHP’s current OOP engine is quite good enough yet. Also after coming from PHP OOP to Ruby and Python OOP I found out just how many bad habit and techniques I picked up along the way! πŸ™‚ But this is a really good base introduction to OOP with PHP and I wish this had been around when I started learning it in late ’04!

    Keep up the good work guys! I love Think Vitamin!

    P.S You spelled ‘branches’ wrong in your introduction πŸ™‚

  37. OOP can be difficult to get your head around, nice practical examples.

  38. “A common mistake for those coming to OO for the first time is to declare methods inside a class but then instantiate a single copy of that and pass in data such as the ID of the data to each method.” Why is this a mistake? Is it a matter of efficiency?

    Right now I am in hybrid mode, mixing OOP and procedural techniques. Mostly because it seems to take a lot more time for OOP and I don’t clearly understand it and it’s use cases. This article has helped clarify some of it. Perhaps you could put some procedural examples next to OOP examples and show when OOP makes more sense?

    I am a self-taught PHP programmer by the way so there are more then likely some gaps in my knowledge, but I am striving to learn more.

    • I think the implication is that you’d be better off setting the user ID to be a member of the class, which the various methods would reference internally. This way the code would be more consistent with the concept of the object being like an instance of a user. As it stands the class is just used as a container for related functions.

      If the user ID is required I’d make it an argument of the constructor.
      class User
      protected $user_id;

      public function __construct($user_id) {
      $this->user_id = (int)$user_id;

  39. Hi Lorna, a really nice article! Great read!



  40. Martin,

    I don’t see any SQL injection vulnerability issues. It would be the responsibility of the code handling user input to ensure that the data was fit for use by the User class.

    I agree with the rest of your comment though πŸ™‚



    • In user.php, the parameters aren’t escaped. Hence, if something non-numeric was specified for the user ID it would be executed.

      But like I say, that’s a totally different topic and would complicate the examples.

      • Rob is right regarding the fact that the code handling user input would be responsible for the primary validity check, which in this case would be as easy as doing preg_match(“/^[0-9]+/”, $text). In any case the solution here is parameterized sql calls, not escaping user data and appending directly.

    • Basic SQL security should be demonstrated (even if not discussed in the article).
      I disagree that it’s the responsibility of some form validation code. The code at that point doesn’t know where the data will be going, it should not assume it’s heading for an SQL query and escape prematurely (this is the problem with magic_quotes).

      The code running the query is responsible. A simple (int)$user_id or is_numeric($user_id) would do for this example.

  41. Oh, noes! SQL injection vulnerabilities! But that is outside the scope of this tutorial, so can be forgiven…

    This is a really good tutorial. PHP classes and objects is one of those subjects that many try to teach, and many still don’t get after reading. This is one of the clearer and more informative articles/introductions to classes/objects I’ve come across, so kudos for that.

  42. Joachim on March 26, 2010 at 12:17 pm said:

    Thank you so much for this introduction to OOP in PHP. This is exactly what I was looking for several days ago when I started coding a RSS-Reader like application (which is capable of processing feeds data and presenting it in your blogs.)
    Since I weren’t too familiar with OOP in PHP I used to code the old fashioned way with functions in bundles and files named β€˜dosomething.funt.inc’ which blows up your code and ends in a confusing mess.
    But now I know how to improve it and I am going to dig deeper into this.
    Thanks again for this great insight and I am looking forward to part 2…

  43. Good approach to OOP!

Leave a Reply

Want to learn more about PHP?

Learn how to create dynamic websites using the back-end programming language, PHP.

Learn more