PHP OOP —I am the Captain now — 1

Syed Sirajul Islam Anik
3 min readAug 5, 2019

--

The image has been taken from http://bit.ly/2yLxs4Z

I have been working with PHP since mid-2013. So many days have passed by and I came to know a bit about OOP cause of day to day use. I am going to explain to you the way you can be a master in OOP and remember I’m the captain now till you get it all.

This series will mainly focus on tweaks and notes rather than explaining broadly on topics. Sit tight and go through the series of articles. The series is written based on PHP7+.

In this article, few words are used so that it becomes easy to understand to the readers.

Namespace

Namespacing is the way of isolating/separating your classes, functions or constants into a different scope or boundary.

  • Including files don’t flow your namespace import statements to next files.
  • When aliasing the namespaces, you can’t alias the global namespace.
  • Importing an invalid class from a non-valid namespace won’t fire issue any error/exception.
  • Functions and Constants within the namespace use the global functions and constants if they are not present within the namespace. Example: sprintf, fopen.

OOP or OOOOPs

It’s always been asked what are the basic fundamentals of OOP. And the answer is as —

  • Abstraction
  • Inheritance
  • Encapsulation
  • Polymorphism

Abstraction

Abstraction is the way you decide what are the necessary things you need to have for an entity for the outside world.

Inheritance

The concept is to flow the properties from top to the bottom. This feature lets your inherited members derive from parent to the current object. (Objects will be discussed shortly)

Encapsulation

Encapsulation feature lets you decide whether you want it to flow to next inheritance or not. It’s actually the feature that works with the visibility (Visibility will be discussed shortly)

Polymorphism

Polymorphism is a feature that lets you modify your behavior according to your need from what you got from inheritance.

Class

Class is the way of defining your own data structure. It can be referred to as mold or template what can be used to define your idea. There is no way to define a shape with primitive data types. Shapes can have height, weight. To define that you need to create your own data structure which is referred to as a class. An example below. It’s that simple or dead simple.

Class Shape 
{
public $height, $weight;
}

Object

Like the primitive data type, if you want to use a class you will have to instantiate the class. What is instantiating the class? It means building the data from a structure as required. The object instantiation can be done like the following,

$shape = new Shape;
// otherwise,
// $shape = new Shape();

Yes, it’s also that simple. You’re done learning simple stuff now. Let’s try something hard (or even harder).

Constructor

A constructor is the first place where the class is constructed through. In PHP the construction begins from the method __construct (Watch that double underscore). Constructors are like regular methods/functions but it’s a type of a magic method in PHP. Yes, you can call this method from outside your class.

A constructor can have multiple parameters like a regular method and can also have optional parameters too. But, you’ll have to make sure that the parameters are correctly passed to the constructor when instantiating a class.

No, it’s not mandatory to have a constructor always. Only required if you need to initialize the class with some data.

<?php 
// Class declarating with constructor
// For now, ignore the public keyword.
class Shape
{
public $height, $width;

public function __construct ($height, $width) {
$this->height = $height;
$this->width = $width;
}
}
$shape = new Shape(10, 20); // new Shape will not work here

If you need to instantiate a class with some value, you’ll have to instantiate with the parenthesis. But remember one thing, to construct a new object, you should always use new keyword. It’s a must-have to construct a new object.

That’s how you define a class and create an object or instantiate an object from that class. Now you have your class and your object.

Wait for the next article to arrive in no time….

Self-branding: Maybe following me will notify you whenever I publish the next article. :D

--

--

Syed Sirajul Islam Anik

software engineer with "Senior" tag | procrastinator | programmer | !polyglot | What else 🙄 — Open to Remote