Understanding PHP’s OOP – basic terms explained

Understanding PHP’s OOP – basic terms explained

Hello there! I’ll explain some basic terms of PHP’s Object Orientated Programming terms that any developer needs to know. Perhaps you already use all of those, but if you don’t know which is which, you will have a problem in team communication. If so, please read on.

First of all, to understand any PHP OOP concept, you need to know the PHP basics. Then I need you to read and understand next paragraph – Procedural vs OO Programming:

Procedural programming creates a step by step program that guides the application through a sequence of instructions. Each instruction is executed in order. Procedural programming also focuses on the idea that all algorithms are executed with functions and data that the programmer has access to and is able to change. Object-Oriented programming is much more similar to the way the real world works; it is analogous to the human brain. Each program is made up of many entities called objects. Objects become the fundamental units and have behavior, or a specific purpose, associated with them. Objects cannot directly access another object’s data. Instead, a message must be sent requesting the data, just like people must ask one another for information; we cannot see inside each other’s heads.

Basic OOP concepts

Class

As Wikipedia says “A class is a template for an object, a user-defined datatype that contains variables, properties, and methods. You should imagine any class as a wrapper of functions (methods) that will help you manipulate some specific data. Well, I’ll give you an example:

//example filename would be simpleclass.php
< ?php class SimpleClass { // property declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var;
}
}
?>

Here you can see syntax used in declaration of classes and an example method inside it, together with declaration of property.

Method

A method is just an expression for a function bound to a class. You can see an example of method (displayVar method) in declaration of class SimpleClass above.

Instance

Instance is an object (datatype) created by your class. The object consists of state and the behavior that’s defined in the object’s classes. So, take a look at this:

//example filename would be create_instance.php
 
< ?php include "simpleclass.php"; $ourObject = new SimpleClass(); $ourObject->displayVar();
 
?>

In this example “$ourObject = new SimpleClass();” is line of code that created and instance (object) of SimpleClass class. And next line “$ourObject->displayVar();” accesses method of SimpleClass class on $ourObject instance.

Inheritance

Inheritance is a process in which a class inherits all the state and behavior of another class. in PHP, this is achieved when you extend existing class with your own. Take a look:

//example filename would be simpleclass.php
< ?php include "SimpleClass.php"; class MyClass extends SimpleClass { // class property declaration of "name" public $name = null; public function assignValueToObjectVar($val) { $this->name = $val;
}
 
public function printValueFromObjectVar() {
echo $this->name;
}
}
//here we'll create a instance of MyClass:
$myObject = new MyClass();
//we assigned value "Test Name" to object's $name property
$myobject-><code>assignValueToObjectVar('Test Name');
//we called method that echoes value from object's $name property
$myObject->printValueFromObjectVar();
?>

Last part of example is self-explanatory, I hope. 😀

Abstraction

Abstraction refers to the act of representing essential features without including the background details or explanations. It reduces and factors out details so that one can focus on a few concepts at a time. Take a look at php.net‘s example:

< ?php abstract class AbstractClass { // Force Extending class to define this method abstract protected function getValue(); abstract protected function prefixValue($prefix); // Common method public function printOut() { print $this->getValue() . "\n";
}
}
 
class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
return "ConcreteClass1";
}
 
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass1";
}
}
 
class ConcreteClass2 extends AbstractClass
{
public function getValue() {
return "ConcreteClass2";
}
 
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass2";
}
}
 
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";
 
$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_') ."\n";
?>

In this example you can see “abstract level” in declaration of AbstractClass which is then extended by ConcreteClass1 and ConcreteClass2. All of them have same methods but you can add another method to AbstractClass that will do a bit more complex work in both ConcreteClass1 and ConcreteClass2, without the need to write that “universal logic” to both of extend classes.

Singleton

Singleton restricts the instantiation of a class to one object. You can look at the class itself as an instance (object). To give you an example where a singleton is widely used – with database connection. There’s no need to create a new database connection on each class you use in a single moment, but rather, you can use one connection through the project:

class Database
{
// Store the single instance of Database
private static $instance;
 
private function __construct() {
//database connection goes here
self::$instance = $link;
}
 
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new Database();
}
return self::$instance;
}
 
//our method that does something with database
public static function myDbMethod(){...}
}
 
//Usage of singleton classes differ a bit from classic approach
//where you create n instance and then do something with it.
$DB = Database::getInstance();
$Result = $DB->myDbMethod();

I hope this will help with the understanding of basics needed for any actual object orientated programming.

Cheers!

 

Sources: Wikipedia, php.net

You made it all the way down here so you must have enjoyed this post! You may also like:

How to programmatically create customers in Magento 2.3.x Deni Pesic
Deni Pesic, | 2

How to programmatically create customers in Magento 2.3.x

90% of Magento websites are running on unsupported PHP versions. Why is this a problem and how can you (we) solve it? Aron Stanic
Aron Stanic, | 9

90% of Magento websites are running on unsupported PHP versions. Why is this a problem and how can you (we) solve it?

Development environment for Magento 2 using Docker Tomas Novoselic
, | 14

Development environment for Magento 2 using Docker

1 comment

  1. Hello!
    Thanks for your article! It’s very useful.
    I think that it’d be an error in the Inheritance’s code:
    $myobject->assignValueToObjectVar('Test Name');
    instead of
    $myObject->assignValueToObjectVar('Test Name');

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.