PHP & OOP: The introduction

This entry is part 1 of 11 in the series PHP & OOP

Object-oriented programming is an approach to designing modular, reusable software systems.

Just like C, PHP was started out as a non object-oriented programming language.

Started in version 4, some basic elements of object oriented features were added.

And in version 5, especially from v 5.3, it started to support most features of object oriented methodology.

In some of our earlier examples, we have used object oriented style of programming.

You can refer to this example where I have used:

$mysqli = new mysqli('localhost', 'root', '', 'allartist')
$mysqli->query($query)
$result->fetch_assoc()

We could write those code in procedural style but learning about OOP will open up a whole world where we could reuse OOP code written by others.

PHP & OOP: The OOP core ideas

This entry is part 2 of 11 in the series PHP & OOP

What made a language object-oriented?

There are a few core ideas related to Object Oriented programming language.

  • Dynamic Dispatch – when a method is invoked on an object,
  • Encapsulation – in which case the state is kept separate
  • Inheritance – inheritance lets the structure and methods in one class pass down the hierarchy

Additional concepts are classes and objects and we will start off our series with classes and objects.

PHP & OOP: A simple class declaration

This entry is part 3 of 11 in the series PHP & OOP

We first look at this simple class declaration.

<?php
class SimpleClass
{
    // property declaration
    public $var = 'a default value';

    // method declaration
    public function displayVar() {
        echo $this->var;
    }
}

$simple_object = new SimpleClass();
echo $simple_object->displayVar();

?>

In object-oriented terminology, a property is actually the variable of a class and a method is a function declared inside the class.

In the above example, we have used the method displayVar() to access the variable $var.  The variable $var is a property of the class SimpleClass.

Visibility

The visibility or scope of a property or method can be defined by prefixing the declaration with the keywords public, protected or private.

Class members declared public can be accessed everywhere.

Members declared protected can be accessed only within the class itself and by inherited and parent classes.

Members declared as private may only be accessed by the class that defines the member.

PHP & OOP: Instance of a class

This entry is part 4 of 11 in the series PHP & OOP

The keyword new is to create an instance of a class.

<?php
class SimpleClass
{
    public $var = 'a default value';

    public function displayVar() {
        echo $this->var;
    }
}

//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayVar();

?>

As in the line

$simple_object = new SimpleClass();

You can also think of it as creating an object from the class SimpleClass.  With the instance $simple_object, we can access the class variable using

$simple_object->displayVar()

In PHP, we have used -> operator.  In other programming languages such as C++, we would have used the dot(.) operator.

PHP & OOP: $this variable

This entry is part 5 of 11 in the series PHP & OOP

In this example, the $this variable is a reference to the current object, which is itself, SimpleClass.  The this reference is commonly found in other object-oriented programming languages.

<?php
class SimpleClass
{
    public $greeting = 'Hi There';
 
    public function displayGreeting() {
        echo $this->greeting;
    }
}
 
//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayGreeting();
 
?>

The $this->greeting is to access its own variable $greeting.

If you access the $greeting variable without the $this reference, the browser will throw an undefined variable greeting error message.

You can try the below code to see the error message.

<?php
class SimpleClass
{
    public $greeting = 'Hi There';
 
    public function displayGreeting() {
        echo $greeting;
    }
}
 
//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayGreeting();
 
?>

this_error