1. What is OOP?
OOP stands for Object-Oriented Programming, which is a programming paradigm that focuses on creating objects that encapsulate data and behavior. It is a way of organizing code that is more modular, maintainable, and reusable.
2. What is object?
An object is an instance of a class, which represents a specific entity or concept. It contains data (called properties or attributes) and functions (called methods) that operate on that data. An object is created from a class blueprint and can be used to perform actions and interact with other objects in the system.
3. Explain class with example.
A class is a blueprint for creating objects. It defines the properties and methods that all objects of that class will have. Here is an example of a class in PHP:
class Person {
public $name;
public $age;
public function greet() {
echo "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
}
}
This class represents a person with a name and an age. It has one method called "greet" that outputs a message with the person's name and age.
4. How to create an object? Explain with example.
To create an object, you need to instantiate a class using the new
keyword. Here's an example:
// Create a new Person object
$person = new Person();
// Set the name and age properties
$person->name = "John";
$person->age = 30;
// Call the greet method
$person->greet();
This code creates a new Person
object, sets its name and age properties, and then calls the greet
method to output a greeting message. The output would be: "Hello, my name is John and I am 30 years old."
5. What is inheritance?
Inheritance is a mechanism in object-oriented programming that allows one class to inherit properties and methods from another class. The class that is being inherited from is called the parent or base class, and the class that is inheriting is called the child or derived class. Inheritance allows for code reuse and promotes a hierarchical organization of code.
6. Describe method overloading with example.
Method overloading is a feature in PHP that allows a class to define multiple methods with the same name but different parameters. The correct method to call is determined at runtime based on the number and type of arguments passed to it.
Here's an example:
class Calculator {
public function add($num1, $num2) {
return $num1 + $num2;
}
public function add($num1, $num2, $num3) {
return $num1 + $num2 + $num3;
}
}
In this example, the Calculator
class defines two methods called add
, one with two parameters and one with three parameters. When you call the add
method with two arguments, the first method will be called, and when you call it with three arguments, the second method will be called.
7. Explain sleep and wakeup functions?
The __sleep
and __wakeup
functions are magic methods in PHP that are used for object serialization and deserialization.
When an object is serialized (for example, when it is stored in a file or sent over a network), the __sleep
function is called. This function should return an array of property names that should be serialized. Any properties that are not returned in the array will not be serialized.
When an object is deserialized (for example, when it is read from a file or received over a network), the __wakeup
function is called. This function can be used to restore any state that was not serialized or to perform any other initialization that needs to be done after deserialization.
Here's an example:
class Person {
public $name;
public $age;
public function __sleep() {
return ['name'];
}
public function __wakeup() {
$this->age = 30;
}
}
// Create a new Person object
$person = new Person();
$person->name = "John";
$person->age = 40;
// Serialize the object
$data = serialize($person);
// Unserialize the data
$person2 = unserialize($data);
// Output the name and age properties
echo $person2->name; // Output: John
echo $person2->age; // Output: 30
In this example, the Person
class defines the __sleep
function to only serialize the name
property, and the __wakeup
function to set the age
property to a default value of 30 if it was not serialized. When the object is serialized and then unserialized, the name
property is restored, but the age
property is initialized to 30.
8. What is serialization in PHP?
Serialization in PHP is the process of converting a PHP object or variable into a format that can be stored or transmitted, such as a string, file, or network stream. This process is also known as object serialization or object marshaling. Serialized data can later be restored back into an object or variable using the unserialize function.
Here's an example of serializing an object in PHP:
class Person {
public $name;
public $age;
}
// Create a new Person object
$person = new Person();
$person->name = "John";
$person->age = 30;
// Serialize the object
$data = serialize($person);
// Output the serialized data
echo $data;
In this example, the Person
object is serialized using the serialize
function, which returns a string containing the serialized data. The output would be something like this: O:6:"Person":2:{s:4:"name";s:4:"John";s:3:"age";i:30;}
.
9. What is introspection? Explain in detail.
Introspection is a technique in computer science that allows a program to examine and manipulate its own internal structure and behavior at runtime. In PHP, introspection is achieved through a set of built-in functions that can be used to query information about classes, functions, objects, and variables.
Some common examples of PHP introspection functions include get_class
, method_exists
, property_exists
, get_object_vars
, and get_class_methods
. These functions can be used to inspect the structure and behavior of objects and classes, and to dynamically invoke methods and access properties.
Introspection is often used in PHP frameworks and libraries to implement features such as automatic dependency injection, dynamic method dispatch, and runtime code analysis. It can also be used for debugging, testing, and other development tasks that require runtime introspection.
10. Define method overriding.
Method overriding is a feature in object-oriented programming that allows a subclass to provide a different implementation for a method that is already defined in its parent class. When a method is overridden, the subclass version of the method is used instead of the parent version when the method is called on an instance of the subclass.
Here's an example:
class Shape {
public function area() {
return 0;
}
}
class Rectangle extends Shape {
private $width;
private $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
public function area() {
return $this->width * $this->height;
}
}
// Create a new Rectangle object
$rectangle = new Rectangle(5, 10);
// Call the area method
echo $rectangle->area(); // Output: 50
In this example, the Shape
class defines a method called area
that returns 0. The Rectangle
class extends the Shape
class and provides its own implementation of the area
method that calculates the area of a rectangle. When the area
method is called on an instance of the Rectangle
class, the subclass version of the method is used instead of the parent version.
11. Compare class and object.
In PHP, a class is a blueprint or template for creating objects. It defines the properties and methods that are common to all instances of the class. An object, on the other hand, is an instance of a class that has been created using the new
keyword. Objects can have their own unique values for the properties defined by the class, and can call the methods defined by the class.
Here's an example:
class Person {
public $name;
public $age;
public function sayHello() {
echo "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
}
}
// Create a new Person object
$person = new Person();
// Set the properties of the object
$person->name = "John";
$person->age = 30;
// Call the sayHello method
$person->sayHello(); // Output: "Hello, my name is John and I am 30 years old."
In this example, the Person
class defines two properties ($name
and $age
) and a method (sayHello
) that prints out a greeting message. The new
keyword is used to create a new instance of the Person
class, and the object's properties are set using the object notation. Finally, the sayHello
method is called on the object.
12. What is object cloning? Describe with example.
Object cloning is a feature in PHP that allows you to create a new object that is a copy of an existing object. The clone
keyword is used to create a new instance of the object, and the new instance has the same property values as the original object.
Here's an example:
class Person {
public $name;
public $age;
public function __clone() {
$this->age = 0;
}
}
// Create a new Person object
$person1 = new Person();
$person1->name = "John";
$person1->age = 30;
// Clone the object
$person2 = clone $person1;
// Modify the cloned object
$person2->name = "Jane";
$person2->age = 25;
// Output the properties of the objects
echo $person1->name . " is " . $person1->age . " years old."; // Output: "John is 30 years old."
echo $person2->name . " is " . $person2->age . " years old."; // Output: "Jane is 25 years old."
In this example, the Person
class defines two properties ($name
and $age
) and an __clone
method that sets the age property of the cloned object to 0. The new
keyword is used to create a new instance of the Person
class, and the object's properties are set using the object notation. The clone
keyword is then used to create a copy of the original object, and the name
and age
properties of the cloned object are modified. Finally, the properties of the original and cloned objects are output to the screen.
13. Define the terms:
- Object: An object is an instance of a class in object-oriented programming (OOP). It contains data in the form of properties and can perform actions in the form of methods. Objects allow for code to be structured in a modular and reusable way.
- Class: A class is a blueprint or template for creating objects in object-oriented programming. It defines the properties and methods that an object will have, but does not actually create any instances of the object. Objects are created by instantiating a class using the
new
keyword. - Inheritance: Inheritance is a feature of object-oriented programming that allows a class to inherit properties and methods from another class. The class that inherits from another class is called the subclass, while the class that is inherited from is called the superclass. The subclass can add new properties and methods, or override the properties and methods of the superclass.
- Constructor: A constructor is a special method that is called automatically when an object is created. It is used to initialize the object's properties and can be used to perform any other initialization tasks. In PHP, the constructor method is named
__construct()
.
14. Explain the term destructor with example.
Destructor: A destructor is a special method in PHP that is automatically called when an object is destroyed or goes out of scope. Its main purpose is to perform any cleanup tasks or release any resources that the object was using. In PHP, the destructor method is named __destruct()
.
Here is an example of a class with a destructor method:
class Example {
public function __construct() {
echo "Constructor called.";
}
public function doSomething() {
echo "Doing something...";
}
public function __destruct() {
echo "Destructor called.";
}
}
$obj = new Example(); // Output: "Constructor called."
$obj->doSomething(); // Output: "Doing something..."
unset($obj); // Output: "Destructor called."
In this example, the Example
class has a constructor method that is called when a new object is created, and a destructor method that is called when the object is destroyed. The doSomething()
method simply outputs a message. When the script is executed, the constructor method is called when the object is created, the doSomething()
method is called, and the destructor method is called when the object is destroyed using the unset()
function.
16. Differentiate between constructor and destructor.
Both constructors and destructors are special methods in PHP, but they serve different purposes. Here are the main differences between the two:
- A constructor is called when an object is created, while a destructor is called when an object is destroyed or goes out of scope.
- A constructor is used to initialize the object's properties and perform any other initialization tasks, while a destructor is used to perform any cleanup tasks or release any resources that the object was using.
- Constructors have the same name as the class (i.e.
__construct()
), while destructors have the name__destruct()
.
In summary, constructors are used to set up an object, while destructors are used to tear it down.
16. Compare object and class.
- A class is a blueprint or template that defines the properties and methods of an object, while an object is an instance of a class that has its own unique properties and can perform actions using its methods.
- A class is a general concept, while an object is a specific instance of that concept.
You can create multiple objects from the same class, each with its own unique set of properties and values, but there is only one class definition.
17. How to examine classes in introspection? Explain with example.
Introspection is the ability to examine an object and retrieve information about its properties, methods, and other characteristics at runtime. In PHP, you can use a variety of built-in functions to examine classes at runtime, including get_class()
, get_class_methods()
, get_class_vars()
, and is_subclass_of()
.
Here is an example of how to use some of these functions to examine a class:
class Example {
public $prop1;
private $prop2;
public function __construct() {
$this->prop1 = "Hello";
$this->prop2 = "World";
}
public function method1() {
echo $this->prop1 . " " . $this->prop2;
}
private function method2() {
echo "This is a private method.";
}
}
$obj = new Example();
$className = get_class($obj);
$methods = get_class_methods($className);
$vars = get_class_vars($className);
$subClass = is_subclass_of($obj, "Example");
echo "Class name: " . $className . "<br>";
echo "Methods: " . implode(", ", $methods) . "<br>";
echo "Properties: " . implode(", ", array_keys($vars)) . "<br>";
echo "Is subclass of Example? " . ($subClass ? "Yes" : "No");
In this example, we define a class Example
with some properties and methods, and create an instance of it using $obj = new Example();
. We then use the get_class()
function to get the name of the class, and the get_class_methods()
and get_class_vars()
functions to get the list of methods and properties defined in the class. Finally, we use the is_subclass_of()
function to determine if the object is a subclass of the Example
class. The output of this example will show the class name, methods, properties, and whether or not the object is a subclass of Example
.