Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

19. Advanced OOP

Lesson 16 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Magic Method: __set()

The __set() method is called automatically when assigning a value to an inaccessible property.

class Test {
   private $data;

   public function __set($name, $value){
      $this->$name = $value;
   }
}
$obj = new Test();
$obj->data = 10;
Exam Tip: __set() is used for controlled property assignment.

---

Object Cloning and Copy

Object Copy

Assigning object copies reference.

$o2 = $o1;

Object Cloning

Creates a new copy.

$o2 = clone $o1;

__clone() Method

class Sample {
   public function __clone() {
      echo "Object Cloned";
   }
}
Exam Line: clone creates a shallow copy of an object.

---

Reflection in PHP

Reflection allows inspecting classes, methods, and properties at runtime.

ReflectionClass Example

$class = new ReflectionClass('Student');
echo $class->getName();

Uses of Reflection

  • Frameworks
  • Debugging
  • Dependency Injection
  • Code analysis
Exam Definition: Reflection is the ability to examine program structure at runtime.

---

Helper Functions (Common OOP Helpers)

FunctionPurpose
get_class()Returns class name
method_exists()Checks method
property_exists()Checks property
is_object()Checks object

---

Advantages of OOP in PHP

  • Code reusability
  • Data security
  • Easy maintenance
  • Better scalability
  • Real-world modeling