OOP Important Topics for Beginners in PHP
OOP (Object Oriented Programming) is a programming design pattern in which we write code using classes, objects rather than the old programming system. We were writing code using functions. OOP is best suited for that type of application which is a scalable or extensive application.
Example: An example we have a project of the multi-login system in which there are several roles such as
1. Admin login
2. Receptionist login
3. Digital Marketing Login
4. Web Developer Login
Here are four different member logins, and each has different functionalities. For such type of application OOP is best because in OOP have some functionalities by using these we can manage big project easily some of the following:
1. Class
2. Objects
3. Inheritance
4. Access Specifier
5. Constructor
1. Class: A class contains variables and methods which is access by the object. A class is defined by using the class keyword, followed by the pair of curly braces.
Example:
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
2. Object: Object plays a significant role in accessing class variables and methods. A class is nothing without an object; the object is created using a new keyword with the class name.
Example:
$apple = new Fruit();
$banana = new Fruit();
Complete Code: Complete code of class and object
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>
Necessary: When we create a class, you should keep one point in mind is that your class name and your file name should be the same, and without an object, you cant access class methods and variables.
3. Inheritance: The inheritance feature of OOP is handy to reduce our code because inheritance support reusability.
To use inheritance in PHP, we use keywords. Inheritance means child class can access the functionality of parent class.
To use inheritance, there are the following steps:
Step1: create the first file Fruit.php
<?php class Fruit { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } public function intro() { echo "The fruit is {$this->name} and the color is {$this->color}."; } } ?>
Step2: create Strawberry.php file
<?php include("includes/fruit.php"); class Strawberry extends Fruit { public function message() { echo "Am I a fruit or a berry? "; } } $strawberry = new Strawberry("Strawberry", "red"); $strawberry->message(); $strawberry->intro(); ?>
4. Access Specifier: Access specifier provides security in OOP. It controls methods and variables where they can be accessed. There are three access specifiers in OOP:
1. Public: the property and methods can be accessed everywhere.
2. Private: the property and methods accessed only in class where it is created.
3. Protected: the property and methods can be accessed by child class from where it is derived.
Example: Database Connection Example of OOP in which we have created private, protected and public.
<?php
class Dbconnect
{
private $_localhost = 'localhost';
private $_user = 'root';
private $_password = '';
private $_dbname = 'oops';
protected $connection;
public function __construct()
{
if(!isset($this-> connection))
{
$this->connection = new mysqli($this->_localhost , $this->_user , $this->_password , $this->_dbname);
if(!$this->connection)
{
echo 'cannot connect' ;
exit;
}
}
return $this->connection;
}
}
?>
Here we have created a private variable because that variable only needs to access within the same class. Protected variable $connection this variable will be used by the child class.
5. Constructor: Constructor is used when you want to access some function at the time of object creation. Constructor is called automatically. When the object is created, there is no need to access it separately like other methods. Constructor is created by __construct() function in PHP.
Example:
<?php
public function __construct()
{
if(!isset($this-> connection))
{
$this->connection = new mysqli($this->_localhost , $this->_user , $this->_password , $this->_dbname);
nbsp;
if(!$this->connection)
{
echo 'cannot connect' ;
exit;
}
}
return $this->connection;
}
?>
Here we have created a constructor to access database connection at the time of object creation on every page.