Beginners Logo
  • होम
  • ताज़ा खबरें
  • करियर गाइडेंस
  • जॉब ओपनिंग्स
  • एडमिशन
  • परीक्षा की जानकारी
  • एडमिट कार्ड
  • रिजल्ट
  • सामान्य ज्ञान
  1. Home
  2. Tutorial
  3. Php
  4. Php Oop

PHP OOP

Certainly! Object-Oriented Programming (OOP) in PHP is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

Here's a brief breakdown of the core concepts of OOP in PHP:

Classes and Objects:

  • Class: Defines the blueprint for an object. It's a template definition of the methods and variables in a particular kind of object.
  • Object: An individual instance of the data structure defined by a class.
class Car {
   public $color;

   public function describe() {
       return "This car is " . $this->color;
   }
}

$redCar = new Car();
$redCar->color = "red";
echo $redCar->describe(); // Outputs: This car is red
   

Properties and Methods:

  • Properties: Variables defined inside a class. Used to hold the state of the object.
  • Methods: Functions defined inside a class. Used to manipulate the state of the object.
class Person {
   public $name;
   public $age;

   public function greet() {
       return "Hello, my name is " . $this->name . " and I'm " . $this->age . " years old.";
   }
}   

Constructor:

  • Special method inside the class which gets called automatically whenever an object of that class is instantiated.
class Book {
   public $title;
   public $author;

   public function __construct($title, $author) {
       $this->title = $title;
       $this->author = $author;
   }

   public function describe() {
       return "This book, {$this->title}, was written by {$this->author}.";
   }
}

$book = new Book("1984", "George Orwell");
echo $book->describe();   

Inheritance:

  • Allows a class (child class) to inherit properties and methods from another class (parent class).
class Vehicle {
   public $wheels;

   public function describe() {
       return "This vehicle has " . $this->wheels . " wheels.";
   }
}

class Bicycle extends Vehicle {
   public function __construct() {
       $this->wheels = 2;
   }
}

$bike = new Bicycle();
echo $bike->describe();  // Outputs: This vehicle has 2 wheels.   

Visibility (Public, Private, Protected):

  • Public: Accessible everywhere.
  • Private: Accessible only within the class in which it's defined.
  • Protected: Accessible within the class in which it's defined and in its child classes.

Static Properties and Methods:

  • Belong to the class rather than any specific instance.
class Counter {
   public static $count = 0;

   public function __construct() {
       self::$count++;
   }

   public static function showCount() {
       return "Total objects created: " . self::$count;
   }
}

$a = new Counter();
$b = new Counter();

echo Counter::showCount();  // Outputs: Total objects created: 2   

Interfaces and Abstract Classes:

  • Interface: Defines a contract of what a class should have (methods). Any class that implements an interface must provide implementations for the methods declared by the interface.
  • Abstract Class: Can't be instantiated on its own and serves as a base for other classes.
  • Polymorphism: It's the ability in OOP to use a class in place of another class if it inherits from it.
  • Encapsulation: It's the act of keeping properties and methods private inside a class and controlling the access to them.

UPCET Exam

UPCET Exam

Click Here

SAAT Exam

SAAT Exam

Click Here

MHT CET Exam

MHT CET Exam

Click Here

IPU CET Exam

IPU CET Exam

Click Here

KCET Exam

KCET Exam

Click Here

COMEDK UG Exam

COMEDK UG Exam

Click Here

VITEEE Exam

VITEEE Exam

Click Here

BITSAT

BITSAT

Click Here

DSAT: Dayanand Sagar Admission Test

DSAT: Dayanand Sagar Admission Test

Click Here

Career In Animation in india

Career In Animation in india

Click Here

Merchant Navy Courses in india

Merchant Navy Courses in india

Click Here

Interior Design Career in india

Interior Design Career in india

Click Here

UGC NET Exam

UGC NET Exam

Click Here

B. Ed Exam

B. Ed Exam

Click Here

AFCAT - Air Force Common Admission Test

AFCAT - Air Force Common Admission Test

Click Here

GATE Exam

GATE Exam

Click Here

Joint Entrance Examination (JEE)

Joint Entrance Examination (JEE)

Click Here

Common Admission Test (CAT)

Common Admission Test (CAT)

Click Here

CDS - Combined Defence Services Exam

CDS - Combined Defence Services Exam

Click Here

अन्य खबरें

  • प्राइवेट नौकरी अपडेट: Testbook में कंटेंट राइटर की नई वैकेंसी, ग्रेजुएट करें अप्लाई, दिल्ली में मिलेगी फुल टाइम जॉब

  • मार्च में सरकारी नौकरी के सुनहरे मौके! बैंक, पुलिस, डाक विभाग से लेकर असिस्टेंट प्रोफेसर तक कई भर्तियाँ जारी

  • केवीएस बाल वाटिका एडमिशन 2025: केंद्रीय विद्यालय में नर्सरी, एलकेजी, यूकेजी में प्रवेश शुरू, आवेदन प्रक्रिया और अंतिम तिथि जानें

  • नीट यूजी 2025 परीक्षा रजिस्ट्रेशन शुरू: फीस, एडमिट कार्ड, परीक्षा तिथि और रिजल्ट से जुड़ी सभी जरूरी जानकारी

  • यूपी बीएड प्रवेश परीक्षा 2025: आवेदन की तारीख घोषित, जानें रजिस्ट्रेशन प्रक्रिया, परीक्षा तिथि और अन्य जरूरी जानकारी

  • Bihar DElED Exam 2025: डीएलएड अभ्यर्थियों के लिए जरूरी अपडेट, आवेदन में हुई गलती ऐसे करें सुधार, अंतिम तिथि से पहले पूरा करें प्रक्रिया

  • PHP Tutorial
    • PHP Tutorial
    • PHP Introduction
    • PHP Installation
    • PHP Syntax
    • PHP Comments
    • PHP Variables
    • PHP Echo / Print
    • PHP Data Types
    • PHP Strings
    • PHP Numbers
    • PHP Math
    • PHP Constants
    • PHP Operators
    • PHP If...Else...Elseif
    • PHP Switch
    • PHP Loops
    • PHP Functions
    • PHP Arrays
    • PHP Superglobals
    • PHP RegEx
    • PHP Forms
    • PHP Form Handling
    • PHP Form Validation
    • PHP Form Required
    • PHP Form URL/E-mail
    • PHP Form Complete
    • PHP Advanced
    • PHP Date and Time
    • PHP Include
    • PHP File Handling
    • PHP File Open/Read
    • PHP File Create/Write
    • PHP File Upload
    • PHP Cookies
    • PHP Sessions
    • PHP Filters
    • PHP Filters Advanced
    • PHP Callback Functions
    • PHP JSON
    • PHP Exceptions
    • PHP OOP
    • PHP What is OOP
    • PHP Classes/Objects
    • PHP Constructor
    • PHP Destructor
    • PHP Access Modifiers
    • PHP Inheritance
    • PHP Abstract Classes
    • PHP Interfaces
    • PHP Traits
    • PHP Static Methods
    • PHP Static Properties
    • PHP Namespaces
    • PHP Iterables
    • MySQL Database
    • MySQL Connect
    • MySQL Create DB
    • MySQL Create Table
    • MySQL Insert Data
    • MySQL Get Last ID
    • MySQL Insert Multiple
    • MySQL Prepared
    • MySQL Select Data
    • MySQL Where
    • MySQL Order By
    • MySQL Delete Data
    • MySQL Update Data
    • MySQL Limit Data
    • AJAX Intro
    • AJAX Database
    • AJAX Live Search
    • AJAX Poll
    • PHP Examples
    • PHP Compiler (Editor)
    • PHP Quiz
    • PHP Exercises
    • PHP Certificate
    • PHP Reference
    • PHP Overview
    • PHP Array
    • PHP Calendar
    • PHP Date
    • PHP Directory
    • PHP Error
    • PHP Exception
    • PHP Filesystem
    • PHP Filter
    • PHP FTP
    • PHP Keywords
    • PHP Mail
    • PHP Misc
    • PHP MySQLi
    • PHP Network
    • PHP Output Control
    • PHP Stream
    • PHP String
    • PHP Variable Handling
    • PHP XML Parser
    • PHP Zip
    • PHP Timezones
  • Other Tutorials
    • Angular
    • CSS
    • Javascript
    • Mysql
    • Node JS
    • PHP Tutorial
    • Python
    • React Tutorial