PHP OOPS - Classs Object

1. PHP OOPS Concept – PHP Class Object

Class

A class is a group of objects with common behavior (Functionality) and properties( Attributes). Class is the blueprint of the object.

Class is virtual

Car Class, Human Class, Byke Class

Behaviour:  Running, Accelerator, steering wheel. 

Properties:  Size, Color

Object

The object is an instance(Example) of the class.

SUV 500 – is an example of a Car class.

Class Car{

   $color =’Red’; // Property

   function run(){

    echo ‘Car is running fast’;   
}
}

In the context of PHP, an object-oriented programming (OOP) language, the principles and features of object-oriented programming are implemented with some specific nuances. Here’s how the features of OOP apply to PHP:

1. Encapsulation

  • PHP allows the combining of data (properties) and methods (functions) into a class.
  • Access to properties and methods can be controlled using visibility keywords:
    • public: Accessible everywhere.
    • protected: Accessible within the class and its child classes.
    • private: Accessible only within the class.

Example:

 

class User {

    private $name; // Encapsulated property

 

    public function setName($name) {

        $this->name = $name;

    }

 

    public function getName() {

        return $this->name;

    }

}

 

$user = new User();

$user->setName(“Alice”);

echo $user->getName(); // Outputs: Alice

2. Inheritance

  • PHP supports inheritance, allowing a child class to inherit properties and methods from a parent class using the extends keyword.

Example:

 

class Animal {

    public function sound() {

        echo “Some sound”;

    }

}

 

class Dog extends Animal {

    public function sound() {

        echo “Bark”;

    }

}

 

$dog = new Dog();

$dog->sound(); // Outputs: Bark

3. Polymorphism

  • PHP supports both method overriding and method overloading (via default arguments).

Method overriding:

 

class Shape {

    public function draw() {

        echo “Drawing shape”;

    }

}

 

class Circle extends Shape {

    public function draw() {

        echo “Drawing circle”;

    }

}

 

$shape = new Circle();

$shape->draw(); // Outputs: Drawing circle

Method overloading (indirectly through magic methods like __call()):
php

 

class Test {

    public function __call($name, $arguments) {

        echo “Calling $name with arguments: ” . implode(“, “, $arguments);

    }

}

 

$obj = new Test();

$obj->example(1, 2, 3); // Outputs: Calling example with arguments: 1, 2, 3

4. Abstraction

  • PHP supports abstraction using abstract classes and interfaces:
    • Abstract Class: This cannot be instantiated and can be implemented using both the abstract method and the
    • Interface: Defines method signatures without implementation.

Example:

 

abstract class Vehicle {

    abstract public function move();

}

 

class Car extends Vehicle {

    public function move() {

        echo “Driving a car”;

    }

}

 

$car = new Car();

$car->move(); // Outputs: Driving a car

5. Classes and Objects

  • PHP allows defining classes using the class keyword and creating objects using the new keyword.

Example:

 


class Person {

    public $name;

}

 

$person = new Person();

$person->name = “John”;

echo $person->name; // Outputs: John

6. Dynamic Binding

  • PHP resolves method calls and property access at runtime.

Example:

 

class ParentClass {

    public function show() {

        echo “Parent method”;

    }

}

 

class ChildClass extends ParentClass {

    public function show() {

        echo “Child method”;

    }

}

 

$obj = new ChildClass();

$obj->show(); // Outputs: Child method

7. Message Passing

  • Objects interact by calling each other’s methods using the -> operator.

Example:

 

class Calculator {

    public function add($a, $b) {

        return $a + $b;

    }

}

 

$calc = new Calculator();

echo $calc->add(5, 3); // Outputs: 8

8. Modularity

  • Classes in PHP provide modularity, allowing code to be organized and reused.
  • PHP supports namespaces to organize code and avoid name collisions.

Example:

 

namespace MyApp;

 

class User {

    public function getInfo() {

        echo “User information”;

    }

}

9. Extensibility

  • PHP allows extending existing classes and adding new functionality through inheritance or by implementing interfaces.

10. Reusability

  • Code reusability is achieved by defining reusable classes, traits, and methods.
  • Traits allow inclusion of shared functionality in multiple classes.

Example:

 

trait Logger {

    public function log($message) {

        echo $message;

    }

}

 

class Application {

    use Logger;

}

 

$app = new Application();

$app->log(“This is a log message”); // Outputs: This is a log message

11. State and Behavior

  • PHP objects maintain state using properties and define behavior using methods.

12. Object Identity

  • Every object in PHP has a unique identity (reference). Comparing two objects using === checks whether they are the same instance.

 

Leave a Reply

Your email address will not be published. Required fields are marked *