Welcome again - we'll be starting a totally new topic now
This article is purely theoretical - I want to familiarize you with OOP and it's basic principles
So, what will we learn today?
- What is OOP
- Basic concepts - like class, object, method, attribute etc.
- Encapsulation
- Inheritance
Sound complicated? It will be fine, don't worry
So - let's go
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 we used it's functions
These functions are called methods - remember that term, as I'll use it often
Variables stored inside objects are called attributes
But how can we create 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 really special methods constructor and destructor
Constructor is a method, executed while creating an object. So 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 just cleaning in general. Luckily working with addresses is not our job in PHP - so we can use it for logging purpose or just don't use 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 assets (attributes or methods in our case) visible only for specified group. So friend classes, everyone or no one.
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 told something about inheritance - what is it?
Inheritance
I wont' be explaining too much here, 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 explanation
<?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 explained a lot about OOP - I have shown you some code and we'll focus on writing next week.
Share your feedback in the comments, check out my blog and this article about hashing and see you soon
Top comments (0)