Welcome again - we’ll learn something new today
This article is purely theoretical - I want to familiarize you with OOP and its basic principles
Today, we’ll cover
- What is OOP
- Basic concepts - like class, object, method, attribute etc.
- Encapsulation
- Inheritance
So, let’s start learning
What is OOP?
OOP stands for Object Oriented Programming - that’s a really common principle in modern coding It’s base is a concept of an object - data structure containing both variables and functions
Do you remember connecting to the database? That’s where we created objects - and then used it’s functions
These functions are called methods - remember this term, as I’ll use it often Variables stored inside objects are called attributes
But how can we create an object? Using classes Class is a recipe for an object. It contains everything. attributes, constants, methods.
class someClass {
// all of the content here
}
There are 2 particulary special methods constructor and destructor
Constructor is a method, executed while creating an object. For example, calling like this:
$db = new PDO([...]);
Invokes a constructor - we can do whatever we want to with this method. I use it to assign values to attributes and log information about it.
But destructor is the opposite of constructor (like name says). - It is executed, when object is destroyed. So mostly in the end. In lower-level languages such as C++ destructors where used to deallocate memory and cleaning in general. Luckily working with addresses is not our job in PHP - so we can use it for logging purpose or just skip it at all
Encapsulation
Here, I’ll use another analogy. Let’s treat class as a mafia family.
They have some assets, like cars, houses, stocks etc. But they don’t want some of them, to be known for others.
That’s why they hide them. Set up fake bank accounts, transfer money, hide cars in garages all around the world and so on.
And that’s what encapsulation is all about - we make these assets (attributes or methods in our case) visible only for specified group
We have 3 basic clauses
public
- Make it available for everyone. Other classes, other instances external code etc.private
- Hide it, it’s visible only inside your class. We don’t want to share it.protected
- only inheriting classes can access this (So in our analogy - children of mafia boss)
We add it before declaration of method/attribute inside class
public $var;
private function privateFunction(){ /* more code here */}
But I’ve mentioned something about inheritance - what is it?
Inheritance
I won’t be explaining too much here myself, as I found (in my opinion) the best explanation for this concept in comments of PHP docs
So thank you Mohammad Istanbouly for this piece of code
<?php
class Person
{
public $name;
protected $age;
private $phone;
public function talk(){
//Do stuff here
}
protected function walk(){
//Do stuff here
}
private function swim(){
//Do stuff here
}
}
class Tom extends Person
{
/*Since Tom class extends Person class this means
that class Tom is a child class and class person is
the parent class and child class will inherit all public
and protected members(properties and methods) from
the parent class*/
/*So class Tom will have these properties and methods*/
//public $name;
//protected $age;
//public function talk(){}
//protected function walk(){}
//but it will not inherit the private members
//this is all what Object inheritance means
}
Conclusion
I hope this article has explained something about OOP I have shown you some code, but we’ll focus on writing next time
Share your feedback in the comments, check out this article about hashing and see you soon