This content originally appeared on DEV Community and was authored by Bazeng
OOP stands for Object-Oriented Programming. Object-oriented programming is about creating objects that contain both data and functions, this is different from Procedural programming which is about writing procedures or functions that perform operations on the data.
Why OOP?
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- OOP helps to keep the PHP code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
- OOP makes it possible to create full reusable applications with less code and shorter development time
Classes & Objects
Classes and objects are the two main aspects of object-oriented programming.
For example
So, a class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
Class
Let assume we have a class Person, a person has a name, age and gender.
To define a class, use curly braces.In a class, variables are called properties and functions are called methods!
<?php
class Person{
//properties of the class
public name;
public age;
public gender;
//methods of the class
function setName($name){
$this->name = $name;
}
function getName(){
return $this->name;
}
function setAge($age){
$this->age = $age;
}
function getAge(){
return $this->age;
}
function setGender($gender){
$this->gender = $gender;
}
function getGender(){
return $this->gender;
}
}
?>
Objects
Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.
Objects of a class is created using the new
keyword.
<?php
$person1 = new Person()
$person1->setName('samuel')
echo "Name:" .$person1->getName()
?>
The $this
Keyword
The $this
keyword refers to the current object, and is only available inside methods.
PHP - instanceof
You can use the instanceof
keyword to check if an object belongs to a specific class:
<?php
$person1= new Person();
var_dump($person1 instanceof Person);
?>
The __construct
Function
A constructor
allows you to initialize an object's properties upon creation of the object.
If you create a __construct()
function, PHP will automatically call this function when you create an object from a class.
<?php
class Person{
//properties of the class
public name;
public age;
public gender;
//methods of the class
function __construct($name,$age,$gender) {
$this->name = $name;
$this->age = $age;
$this->gender = $gender;
}
function getName(){
return $this->name;
}
function getAge(){
return $this->age;
}
function getGender(){
return $this->gender;
}
}
$person1 = new Person("Samuel", 18, "Male");
echo $person->getName();
echo "<br>";
echo $person->getAge();
echo "<br>";
echo $person->getGender();
?>
The __destruct
Function
A destructor
is called when the object is destructed or the script is stopped or exited.
If you create a __destruct() function, PHP will automatically call this function at the end of the script.
<?php
class Person{
//properties of the class
public name;
public age;
public gender;
//methods of the class
function __construct($name,$age,$gender) {
$this->name = $name;
$this->age = $age;
$this->gender = $gender;
}
function __destruct() {
echo "Hello {$this->name}, you are a {$this-
>gender} who is {$this-
>age} old.";
}
}
$person1 = new Person("Samuel", 18, "Male");
Access Modifiers
Properties and methods can have access modifiers which control where they can be accessed.
There are three access modifiers:
public
- the property or method can be accessed from everywhere. This is default
protected
- the property or method can be accessed within the class and by classes derived from that class
private
- the property or method can ONLY be accessed within the class
Check out the example below where we use the access modifiers on properties:
<?php
class Person{
//properties of the class
public name;
protected age;
private gender;
?>
$person1 = new Person();
$person1->name = 'Samuel';//OK
$person1->age = 18;//Error
$person1->gender = 'Male';//Error
Check out the example below where we use the access modifiers on the methods:
<?php
class Person{
//properties of the class
public name;
public age;
public gender;
//methods of the class
function setName($name){
$this->name = $name;
}
protected function setAge($age){
$this->age = $age;
}
private function setGender($gender){
$this->gender = $gender;
}
}
$person1= new Person();
$person1->setName('Samuel'); // OK
$person1->setAge(18); // ERROR
$person1->setGender('Male'); // ERROR
?>
Inheritance
This content originally appeared on DEV Community and was authored by Bazeng
Bazeng | Sciencx (2021-10-26T07:30:03+00:00) PHP OOP. Retrieved from https://www.scien.cx/2021/10/26/php-oop/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.