What is oops in PHP
Oops stands for object-oriented programming language. It is also known as a procedural programming language. It is a programming approach or paradigm that gives us prime consideration of the data and its associated functions. with the help of we can perform operations on data.
It allows you to structure your code more organized and modularly, making it easier to manage and maintain.
It has several advantages.
- It is faster and easier to execute.
- It provides us with the standard structure of programming language.
- It helps us to debug, make it easy to maintain, modify the code and work on the DRY principle ( Don’t Repeat Yourself ).
- It reduces the code with a shorter time.
- It provides higher security and less exposure to the data.
- The object-oriented is used to describe the application’s functionality.
What is a class in PHP?
In PHP, we can define a class by using the “class” keyword. It encapsulates the properties (attributes) and behaviours (methods).
The class is the blueprint or a set of instructions to build a specific type of object.
- The class is a template for objects.
- The class is an entity that is used to determine how an object will behave and what the object will contain.
- The class is used to hold the objects along with their behavior and properties.
- It is a programmer-defined data type, that includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.
Example1
<?php
class Car {
public function name() {
echo " This is the Simple Example of class";
}
}
$obj= new Car;
$obj->name();
?>
Output
This is a Simple Example of a class
Example 2
<?php
class Fruit {function name($parm) {
echo $parm;
}
}
$apple = new Fruit();
echo $apple->name("Mango");
?>
Output
Mango
Example 3
<?php
class Car{
private $name= "My Fav Car Hyundai Creta";
public function display(){
echo $this->name;
}
}
$obj = new Car();
$obj->display();
?>
Output
My Fav Car Hyundai Creta
What is an object in PHP?
In PHP, objects can be related to the entities in real life It considers everything that we can see around us, as an object that has some attributes. An object is an instance of a class. It's created from a class blueprint using the new keyword followed by the class name, optionally passing arguments to the constructor if it's defined. Objects have properties (attributes) and methods defined by their class.
Multiple objects can be created from the same class, each having its own set of property values. Objects allow you to work with specific instances of a class, allowing for data encapsulation and code reuse.
- An object is an instance of class.
- Objects have properties (attributes) and methods defined by their class.
- An object is something that is used to perform a set of related activities.
- We can define a class once and then create one or more objects that belong to it.
- Multiple objects can be created from the same class, each having its own set of property values.
- An Object is an individual instance of the data structure defined by a class.
- Class methods and properties can be accessed directly through object instances.
- We we use the connector (Dot operator->) symbol to connect objects to their properties or methods.
<?php
class Office
{
// Class properties
// methods go here
}
$obj = new Office;
var_dump($obj);
?
<?php
class Office{
public function Chair_name(){
echo "This is Desk Chair"."<br>";
}
public function price(){
echo "RS- 500";
}
}
$obj = new Office();
$obj->chair_name();
$obj->price();
?>
<?php
class Room
{
private $name= "Kitchen Room";
public function display(){
echo $this->name;
}
}
$obj = new Room();
$obj->display();
? >
What is a Constructor in PHP?
<?php
class Car{
function CarModel(){
echo "This is the CarModel function. It will automatically invoke";
}
function Car(){
echo "User Defined Constructor (Class name and function Car() name is same )"."<br>";
}
}
$obj= new Car();
$obj->CarModel();
?>
<?php
class Car{
function CarName(){
echo "Car Name- Thar";
}
function __construct(){
echo "This is predefined constructor"."<br>";
}
}
$obj= new Car();
$obj->CarName();
?>
<?php
class Car{
public $name;
function __construct($name){
$this->name = $name;
}
function get_name(){
return $this->name;
}
}
$obj= new Car("THAR");
echo $obj->get_name();
?>
What is a destructor in PHP?
In PHP, a destructor is a special type of method or function within a class.
It is automatically called when an object is destroyed or goes out of scope or the script is stopped or exited.
It is defined by using the __destruct() method name.
It will be automatically call the function at the end of the script.
It is typically used to perform cleanup tasks like closing files or database connections before an object is destroyed.
<?php
class Car {
public function __construct() {
echo "Constructor called"."<br>";
}
public function __destruct() {
echo "Destructor called";
}
}
$obj = new Car(); // Output: Constructor called
unset($obj); // Output: Destructor called
?>
<?php
class Car {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "My Car name is : {$this->name}.";
}
}
$obj = new Car("Thar");
?>
<?php
class car{
public function car(){
echo "User defined constructor1"."<br>";
}
}
class car2 extends car{
public function __construct(){
echo parent::car();
echo "User Defined Extended constructor2"."<br>";
}
public function __destruct(){
echo "Destroy";
}
}
$obj= new car2();
?>
What is an Access Modifier in PHP?
- Public
- Protected
- Private
1. Public Access Modifier
public access modifier is default, we can access the property or method from everywhere.
In other words, we can say that it is open to use and access inside the class definition and outside the class definition.
Public access modifiers can be accessed publicly, even from outside or inside of the scope of the class.
Example 1
<?php
class Car {
public $name;
protected $color;
private $Model;
}
$obj = new Car();
echo $obj->name = 'THAR'; // OK
echo $obj->color = 'RED'; // ERROR
echo $obj->weight = 'LX Hard Top trim'; // ERROR
?>
<?php
class Car{
public $name="RED THAR";
function display(){
echo $this->name."<br/>";
}
}
class Model extends Car{
function show(){
echo $this->name;
}
}
$obj= new Model;
echo $obj->name."<br>";
$obj->display();
$obj->show();
?>
2. Protected Access Modifier
<?php
class Test{
protected $a=200;
protected $b=300;
function add(){
echo "Addition of (A+B): ".$sum=$this->a+$this->b."<br/>";
}
}
class childtest extends test{
function sub(){
echo "Subtraction of (A-B): ".$sub=$this->a-$this->b."<br/>";
}
}
$obj= new childtest;
$obj->add();
$obj->sub();
?>
3. Private Access Modifier
<?php
class Car{
private $name="THAR CAR";
private function show(){
echo "This is private method of parent class";
}
}
class Model extends Car{
function show1(){
echo $this->name;
}
}
$obj= new Model();
$obj->show();
$obj->show1();
?>
class parents{
public $name="Thar";
protected $Model="Thar-02913";
private $price=50000;
public function show(){
echo "Welcome : ".$this->name."<br/>";
echo "Profile : ".$this->profile."<br/>";
echo "Salary : ".$this->price."<br/>";
}
}
class childs extends parents{
public function show1(){
echo "My New Car : ".$this->name."<br/>";
echo "Model : ".$this->profile."<br/>";
echo "Price : ".$this->salary."<br/>";
}
}
$obj= new childs;
$obj->show1();
?>
<?php
class parents{
public $name="Thar";
protected $model="Thar-02913";
private $price=50000;
public function show(){
echo "Welcome : ".$this->name."<br/>";
echo "Profile : ".$this->model."<br/>";
echo "Salary : ".$this->price."<br/>";
}
}
class childs extends parents{
public function show1(){
echo "My New Car : ".$this->name."<br/>";
echo "Model : ".$this->model."<br/>";
echo "Price : ".$this->salary."<br/>";
}
}
$obj= new childs;
$obj->show1();
?>
What is Encapsulation in PHP?
In PHP, encapsulation is a fundamental concept in object-oriented programming (oops) that binds the data and methods within a class. It provides us protective barrier around the internal workings of an object.
In PHP, encapsulation means wrapping up data members and methods (functions) in a single module. In other words, it is a technique of binding data members and methods.
Hiding the essential internal property of that module is known as data abstraction.
When we wrap up data members and methods together into a single unit that is called Encapsulation.
It allows us to change class to internal implementation without affecting the overall functioning of the system.
- Data Hiding
- Code Organization and Maintainability
- Code Reusability
- Abstraction
- Security and Validation
- Flexibility and Extensibility
- Increased Complexity
- Overhead in Performance
- Dependency on Class Structure
- Limited Accessibility
- Potential for Code Duplication
- Testing Challenges
<?php
class car{
public $name;
public $price;
function __construct($n, $p) {
$this->name=$n;
$this->price=$m;
}
public function setPrice($pr){
$this->pr=$pr;
}
public function display(){
echo "We have Kids Toy : ".$this->name."<br/>";
return $this->color=$this->pr;
}
}
// Create a Car object and set Car name, price
$obj=new car("THAR",5000000);
// Update the car price setPrice() method
$obj->setPrice(900000);
// Display the updated Price
echo "Price: ".$obj->display();
?>
Output
<?php
class calculator{
var $x=100;
var $y=50;
function sum(){
$res=$this->x+$this->y;
echo "Addition (X+Y): ".$res."<br/>";
}
function sub(){
$res=$this->x-$this->y;
echo "Subtraction(X-Y): ".$res."<br/>";
}
function mult(){
$res=$this->x*$this->y;
echo "Multiplication (X*Y): ".$res."<br/>";
}
function div(){
$res=$this->x/$this->y;
echo "Division(x/y) : ".$res."<br/>";
}
}
$obj= new calculator( );
$obj->sum();
$obj->sub();
$obj->mult();
$obj->div();
?>
What is Inheritance in PHP?
There are three main types of inheritance in PHP
- Single Inheritance.
- Multiple Inheritance (Not Supported)
- Multilevel Inheritance.
- 4.Hierarchical Inheritance
<?php
class Parent {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function sound() {
return "Some generic sound";
}
}
class Dog extends Animal {
public function sound() {
return "Woof!";
}
}
$dog = new Dog("Rex");
echo $dog->name . " says " . $dog->sound();
?>
<?php
class Car {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "We can travel with {$this->name} and Its color is {$this->color}";
}
}
// Airplane is inherited from Car
class Airplane extends Car{
public function message() {
echo "You can travel with Airplane- Boeing 747"."<br>";
}
}
$strawberry = new Airplane("Thar", "red");
$strawberry->message();
$strawberry->intro();
?>
<?php
class Animal {
public function sound() {
return "Some generic sound";
}
}
class Dog extends Animal {
public function sound() {
return "Woof!";
}
}
class GermanShepherd extends Dog {
public function sound() {
return "Woof Woof!";
}
}
$gs = new GermanShepherd();
echo $gs->sound();
?>
<?php
class Animal {
public function sound() {
return "Some generic sound";
}
}
class Dog extends Animal {
public function sound() {
return "Woof!";
}
}
class Cat extends Animal {
public function sound() {
return "Meow!";
}
}
$dog = new Dog();
$cat = new Cat();
echo $dog->sound();
echo $cat->sound();
?>
Latest Post
Upload Image in MySQL database using Node js and React js
How to add new column to existing table without losing data in Laravel
Laravel Eloquent display query log in php
What is traits in PHP with example
How to disable click outside modal to close modal in bootstrap
Auto refresh code in HTML using meta tags
How to Optimize the Speed & Performance of a Laravel Website