0% found this document useful (0 votes)
90 views9 pages

Unit - 3 - PHP Question Bank

Unit 3_PHP Question Bank -#PHP #phpnotes #phpquestionbank #bharathidasanuniversity #bharathidasan #BharathidasanUniversity #syllabusphp #phpsyllabus #phpsyllabusbharathidasanuniversity

Uploaded by

Nandhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views9 pages

Unit - 3 - PHP Question Bank

Unit 3_PHP Question Bank -#PHP #phpnotes #phpquestionbank #bharathidasanuniversity #bharathidasan #BharathidasanUniversity #syllabusphp #phpsyllabus #phpsyllabusbharathidasanuniversity

Uploaded by

Nandhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT III PROGRAMMING IN PHP ACAS

Unit -III
1. Object Oriented Programming
a. Creating objects
b. Creating Classes
c. Setting Access to Properties and Methods
d. Constructors
e. Destructors
f. Inheritance
g. Overriding Methods
h. Overloading Methods
i. Autoloading Classes

2. Advanced Object-oriented programming


a. Creating Static Methods
b. Static members and Inheritance
c. Creating Abstract Classes
d. Creating Interfaces
e. Comparing Objects
f. Creating Class constants
g. Using the final Keyword
h. Cloning Objects
i. Reflection

1. Object Oriented Programming


a. Creating Objects:
Definition: In PHP, objects are instances of classes. They allow you to group data
(properties) and functions (methods) together. You create an object using the new keyword
followed by the class name and parentheses.
Example Program:
class Car {
public $brand;
public function startEngine() {
echo "Engine started!";
}
}

$myCar = new Car();


$myCar->brand = "Toyota";
$myCar->startEngine();
 Explanation: The program defines a Car class with a property $brand and a method
startEngine(). An object $myCar of the Car class is created using the new keyword.
The property is set, and the method is called.
 Output:
Engine started!
1
UNIT III PROGRAMMING IN PHP ACAS

b. Creating Classes:
 Definition: A class in PHP is a blueprint for creating objects. It defines the properties
and methods that objects of that class will have.
 Example Program:
class Animal {
public $species;
public function makeSound() {
echo "Animal sound!";
}
}
 Explanation: The program defines an Animal class with a property $species and a
method makeSound(). This class can be used to create objects representing different
animals.
c. Setting Access to Properties and Methods:
 Definition: Access modifiers (public, protected, private) control the visibility of
properties and methods within a class and its subclasses.
 Example Program:
class Person {
public $name;
protected $age;
private $email;
}
 Explanation: In this program, the Person class has properties with different access
modifiers. public properties can be accessed from outside the class, protected
properties can be accessed within the class and its subclasses, and private properties
are only accessible within the class itself.
d. Constructors:
 Definition: Constructors are special methods in a class that are automatically called
when an object is created. They are used to initialize object properties.
 Example Program:
class Book {
public $title;
public $author;

2
UNIT III PROGRAMMING IN PHP ACAS

public function __construct($title, $author) {


$this->title = $title;
$this->author = $author;
}
}

$myBook = new Book("The Great Gatsby", "F. Scott Fitzgerald");


 Explanation: The Book class has a constructor __construct() that initializes the
object's properties when an object is created. The new Book(...) statement calls the
constructor with the provided values.
e. Destructors:
 Definition: Destructors are special methods that are automatically called when an
object is destroyed (e.g., when it goes out of scope). They are used for cleanup tasks.
 Example Program:
class Logger {
public function __destruct() {
echo "Logger instance destroyed.";
}
}

$logger = new Logger();


unset($logger); // Destroy the object
 Explanation: The Logger class has a destructor __destruct() that echoes a message
when the object is destroyed using unset() or when the script finishes.
 Output:
Logger instance destroyed.
f. Inheritance:
 Definition: Inheritance allows you to create a new class that inherits properties and
methods from an existing class (base class or parent class). The new class is called the
derived class or child class.
 Example Program:

3
UNIT III PROGRAMMING IN PHP ACAS

class Shape {
public function calculateArea() {
return 0;
}
}

class Circle extends Shape {


public $radius;

public function calculateArea() {


return 3.14 * $this->radius * $this->radius;
}
}

$circle = new Circle();


$circle->radius = 5;
echo "Circle area: " . $circle->calculateArea();
 Explanation: The program defines a Shape class with a method calculateArea(). A
Circle class is created that extends Shape and overrides calculateArea(). An object
of the Circle class is created and its method is called.
 Output:
Circle area: 78.5
g. Overriding Methods:
 Definition: Overriding allows a subclass to provide a different implementation of a
method that is already defined in its parent class.
 Example Program: (Continuing from the previous "o. Inheritance" example)
class Square extends Shape {
public $side;

public function calculateArea() {


return $this->side * $this->side;

4
UNIT III PROGRAMMING IN PHP ACAS

}
}

$square = new Square();


$square->side = 4;
echo "Square area: " . $square->calculateArea();
 Explanation: The Square class extends Shape and overrides calculateArea(). An
object of the Square class is created, and its method is called.
 Output:
Square area: 16
h. Overloading Methods:
 Definition: Unlike some other programming languages, PHP does not support true
method overloading where you can define multiple methods with the same name but
different parameter lists.
 Example Program: In PHP, you can use default values for function parameters to
achieve a similar effect:
class Math {
public function add($a, $b = 0) {
return $a + $b;
}
}

$math = new Math();


echo $math->add(3, 5); // Output: 8
echo $math->add(7); // Output: 7
 Explanation: In this example, the add() method of the Math class has a parameter
with a default value. This allows you to call the method with one or two arguments.
i. Autoloading Classes:
 Definition: Autoloading allows you to automatically include or require class files
when they are needed, so you don't have to manually include them.
 Example Program:
spl_autoload_register(function ($className) {
include $className . '.php';

5
UNIT III PROGRAMMING IN PHP ACAS

});

$person = new Person();

 Explanation: In this program, the spl_autoload_register() function is used to define a


custom autoloader. When an undefined class (Person in this case) is encountered, PHP
will call the autoloader, and you can include the class file.
 ===========================

2.Advanced Object-oriented programming


a. Creating Static Methods
b. Static members and Inheritance
c. Creating Abstract Classes
d. Creating Interfaces
e. Comparing Objects
f. Creating Class constants
g. Using the final Keyword
h. Cloning Objects
i. Reflection

a. Creating Static Methods:


 Definition: Static methods are methods that belong to the class rather than an instance
of the class. They can be called directly on the class without creating an object.
 Example Program:
class Math {
public static function add($a, $b) {
return $a + $b;
}
}

echo Math::add(3, 5); // Output: 8


Explanation: In this program, the add() method of the Math class is defined as
static. You can call the static method using the :: operator on the class name
without creating an object.

b. Static Members and Inheritance:


 Definition: Static members (properties and methods) are shared among all instances
of a class. Inheritance applies to static members, and subclasses can access and
override them.
 Example Program:
class ParentClass {
public static $staticVar = "Parent";
public static function staticMethod() {
echo "Parent static method";
}

6
UNIT III PROGRAMMING IN PHP ACAS

class ChildClass extends ParentClass {


public static function staticMethod() {
echo "Child static method";
}
}

echo ChildClass::$staticVar; // Output: Parent


ChildClass::staticMethod(); // Output: Child static method

Explanation: In this program, both ParentClass and ChildClass have a static


property and a static method. The child class overrides the static method while still
accessing the static property from the parent class.

c. Creating Abstract Classes:


 Definition: An abstract class is a class that cannot be instantiated and may contain
abstract methods (methods without implementation). Subclasses must provide
implementations for abstract methods.
 Example Program:
abstract class Shape {
abstract public function calculateArea();
}

class Circle extends Shape {


public $radius;

public function calculateArea() {


return 3.14 * $this->radius * $this->radius;
}
}
 Explanation: In this program, the Shape class is abstract and contains an abstract
method calculateArea(). The Circle class extends Shape and provides an
implementation for calculateArea().

d. Creating Interfaces:
 Definition: An interface defines a contract that classes must follow by implementing
its methods. An interface cannot be instantiated; it defines method signatures only.
 Example Program:
interface Logger {
public function log($message);
}

class FileLogger implements Logger {


public function log($message) {

7
UNIT III PROGRAMMING IN PHP ACAS

echo "Logging to file: $message";


}
}

 Explanation: In this program, the Logger interface defines a method log(). The
FileLogger class implements the Logger interface by providing an implementation
for the log() method.
e. Comparing Objects:
 Definition: Objects can be compared using operators like == and ===, which
compare their references. You can also implement custom comparison logic using
methods like __equals().

f. Creating Class Constants:


 Definition: Class constants are values that are associated with a class and are shared
among all instances of the class. They are defined using the const keyword.
 Example Program:
class Math {
const PI = 3.14;
}

echo Math::PI; // Output: 3.14

 Explanation: In this program, the Math class has a constant PI. You can access class
constants using the :: operator on the class name.

g. Using the final Keyword:


 Definition: The final keyword prevents a class from being extended or a method from
being overridden by subclasses.
 Example Program:
class ParentClass {
final public function methodToProtect() {
echo "This method cannot be overridden.";
}
}

class ChildClass extends ParentClass {


// This will result in an error
}
 Explanation: In this program, the methodToProtect() method in ParentClass is
marked as final, so it cannot be overridden in the ChildClass.
h. Cloning Objects:
 Definition: Cloning an object creates a new object with the same properties and
values as the original object. The __clone() method can be used to customize the
cloning process.
 Example Program:

8
UNIT III PROGRAMMING IN PHP ACAS

class Person {
public $name;

public function __clone() {


$this->name = "Cloned " . $this->name;
}
}

$person1 = new Person();


$person1->name = "Alice";

$person2 = clone $person1;

echo $person1->name; // Output: Alice


echo $person2->name; // Output: Cloned Alice
 Explanation: In this program, the Person class has a __clone() method that modifies
the name property when an object is cloned.

i. Reflection:
 Definition: Reflection is an advanced feature in PHP that allows you to examine and
manipulate the structure of classes, properties, and methods at runtime.
 Example Program:
class MyClass {
public $property;

public function method() {


return "Hello from method";
}
}

$class = new ReflectionClass('MyClass');


echo "Class name: " . $class->getName(); // Output: Class name: MyClass

Explanation: In this program, the ReflectionClass is used to examine the structure of


the MyClass class and retrieve its name using the getName() method.

You might also like