It seems like you are using an ad blocker. To enhance your experience and support our website, please consider:

  1. Signing in to disable ads on our site. Our users don't see ads
  2. Or, Sign up if you don't have an account yet
  3. Or, disable your adblocker by doing this:
    • Click on the adblocker icon in your browser's toolbar.
    • Select "Pause on this site" or a similar option for lancecourse.com.

Your First Code In Object Oriented Programming With PHP

By zooboole

Hi everyone, I am back with a new tutorial on Object Oriented Programming(OOP) with PHP.

For some while now I have had the chance to meet many people who have been sending me their source codes to help them with it. I am really pleased to do that.

Through that exercise I have noticed that some people are still working with Procedural programming concepts, and worst some are still using the MySQL extension.

These codes are actually very hard to fix. It looks like each part of them is chained with the other parts. It's almost impossible to test a portion without involving the whole system. I see it like repairing a truck. You have to un-mount each piece before you could be able to access/handle the real problem.

Though these codes seem to be working perfectly, they are not of good practices in the new era of web development -in my opinion. I am convinced today code reuse is key, because web applications are getting bigger and complex. You don't necessary need to create every thing from scratch on every single project. You can easily get other developers' codes and just plug it to your project, and enjoy its power. In case something goes wrong with you application your can easily look at the specific part of it and fix it without breaking down the whole system. That's the spirit in OOP;

PHP give us that ability. In fact, OOP in PHP was feasible since the version 4 and today we are at version 5.6 and soon the version 7 will be out.

OOP has many advantages among which you have:

  • Code reuse
  • code recycling
  • easy maintenance
  • Definition of abstract data types
  • Definition of modular structures
  • etc.

I intend in this tutorial to help you have your first sight on OOP with PHP. In order to do that I would like to take a real use case that we often meet in web development: creating a web page.

What is Object Oriented Programming?

First let's try to understand what OOP is:

According to cognitive science, the human understanding of an object (anything you can "know" something about) consists of two types of knowledge:

  1. Understanding the objects properties or attributes. This data helps you identify what the object is, and distinguish it from other objects of it's type.

  2. Understanding it's uses, behaviors or actions (methods). This information allows you to do something with, or react to the object. | Source

This means an object is first something we can "know" -this is important. It's necessary to understand that it is something we can apprehend or comprehend, but in order to get to that point of understanding we need to get two main things right about the object: its characteristics and its behaviors or manners in which it can be used.

Based on this description they are many common things we know as objects: A car, a house, human being, a computer, etc. They all have their characteristics/attributes and their behaviors or manners in which we can use them or interact with them.

By apprehending things in that angle it become easy to handle them. For example you can say: I want a PC with a screen of 15", a dual code processor, a French keyboard, etc. The same way a client could ask you to create a web page for him with "Happy kids school" as a title, "Happy kids, school for kids, happy kids, happy children" for keywords, and "All kids here are happy, bring yours in", as a description, etc. All these helping you to define the web page, which is an object, but abstract or virtual because you can't touch it, but it's an object.

Create and manipulate objects in PHP

We just saw that we could consider a web page as an object, but how do we translate/write that using PHP.

Good. Since object in programming are almost virtual we have to get a virtual way of representing them using what is called classes. In programming, a class is used to mock up or represent real objects. Once we have the class it's like having a block cutter which can be used to create blocks of the same type.

In our case we want want to have web page(s). First let us create a class for that.

  <?php

    class WebPages{

      // your code will be here

    }

That's it. Now PHP knows that we have a class for web pages, and everytime we need a web page all we have to do is this:

  <?php

    $aWebPage = new WebPages();

You can create as many as you want:

  <?php

    $aWebPage2 = new WebPages();

    $aWebPage3 = new WebPages();

    //etc

We just created some objects from the class WebPages that is used to mock up our web pages. But as you can see these pages look empty and useless, so let's add somethings to the class -tings like the web page properties: title, keywords and description.

  <?php

  class WebPages{

    // creating page attributes
    // PS: should usually be *private*
    public $pageTitle = 'Happy kids school';
    public $keyWords = 'happy kids, happy children';
    public $description = 'All kids are happy here';

  }

  // Let's create a web page now

  $aWebPage = new WebPages();

  // Let's display its title
  echo $aWebPage->pageTitle;

  // You can even change the title without changing the class
  $aWebPage->pageTitle = 'A new page title set';

  echo $aWebPage->pageTitle; // Will display the new title

In all that's how you can use OOP to solve real problems. Notice that once an object is created out of a class we can do all possible things with it without altering the class.

How can I use that in real life?

For a beginner I am sure this looks ambigeous and confusing. Actually there are many way of using OOP to create websites. Let me give an example of how that can be useful.

  • First let's create a folder test
  • In the folder, let's create two files: index.php and webpage-class.php
  • Now enter the following codes for each file:

webpage-class.php

<?php

class WebPages{

  // creating page attributes
  // PS: should usually be *private*
  public $pageTitle = 'Happy kids school';
  public $keyWords = 'happy kids, happy children';
  public $description = 'All kids are happy here';
  public $content = "This is the super content of our web page. Well done!";

}

$indexPage = new WebPages();

index.php

<?php

  include'webpage-class.php';

?>

<!DOCTYPE html>
<html>
  <head>
    <title><?= $indexPage->pageTitle ?></title>
    <meta name="keyword" content="<?= $indexPage->keyWords ?>">
    <meta name="description" content="<?= $indexPage->description ?>">
  </head>

  <body>
    <p>"<?= $indexPage->content ?></p>
  </body>
</html>

Run this page and tell me if that isn't great!

From this point you would have noticed some of its advantages. You can easily modify you html code in the index.php without interfering with the logic of the class in webpage-class.php.

Bottom line

Though this shows you a bit of how you can use OOP in php, it remains one tiny thing in that discipline. There is so much more you can do that we could spend years talking of it. Nevertheless, I would invite you to have a look at the PHP official page that explain OOP in PHP.

They are many things too I did not explain like the -> operator, or the term private, etc. For the purpose of this tutorial just use them the way I did. I expecting to have more tutorials on the topic in the next future, also if you have any other thing you want to understand right now, just as under this tutorial as a comment, I will be glad to give you more details about whatever I said in this post.

Last updated 2024-01-11 UTC