Object Oriented Programming Paradigm in PHP{}

Kabir Ajibade
2 min readAug 18, 2023

--

php oop
php object oriented programming

Object Oriented Programming is a concept of programming that treats all entities as objects. this programming concept is supported by many high level programming languages such as java, c++, c# php etc.

Main aspects of OOP(Major things to know)

Class: A class is a blueprint or framework of an object. Below is an exmple of a php class to connect with a database

class Database{
public $server="127.0.0.1";
public $user="root";
public $database='oopclass';
public $password= '';
public $connection;
public $id;
public function __construct()
{
$this->connection=new mysqli($this->server, $this->user, $this->password, $this->database);
if($this->connection->connect_error){
die("Connection failed:". $this->connection->connect_error);
}
else{
echo "Connection established!";
}
}

public function getConnection(){
return $this->connection;
}
}

Properties: Properties are like variable are in procedural programming. Below are the properties of the class Database

public $server="127.0.0.1";
public $user="root";
public $database='oopclass';
public $password= '';
public $id;

Methods: Methods are the state of functions in a class, in the case of a database Class it can be add, update or delete method.

public function delete(){
//code here
}

Object: An object is an instance of a class. It is declared using the $ followed by name of the object and then the “new” keyword follows followed by the class name.

$db=new Database();
$db->delete($id);

The Class, Properties, Methods and Objects are the four most important aspects of Object Oriented Programming.

Note that these are based on my explanations so far on OOP in php.

--

--