0% found this document useful (0 votes)
9 views18 pages

Unit IV (Objects)

The document provides an overview of objects in PHP, defining an object as an instance of a class that encapsulates properties and methods. It covers creating objects, accessing properties and methods, modifying properties, and using constructors, along with examples of anonymous classes and class inheritance. Additionally, it discusses examining classes and objects using built-in PHP functions to check properties, methods, and parent classes.

Uploaded by

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

Unit IV (Objects)

The document provides an overview of objects in PHP, defining an object as an instance of a class that encapsulates properties and methods. It covers creating objects, accessing properties and methods, modifying properties, and using constructors, along with examples of anonymous classes and class inheritance. Additionally, it discusses examining classes and objects using built-in PHP functions to check properties, methods, and parent classes.

Uploaded by

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

Unit IV(Objects)

Prepared By: Ukesh Bhattarai


Object

 What is an Object?
 An object is an instance of a class that groups related properties and methods.
 An object is a self-contained unit of code that contains attributes
(variables) and behaviors (functions/methods) within a class.
 Objects help organize and structure code in an efficient way.
 Creating an Object
class Car {
public $brand;
public $color;
}

$myCar = new Car(); // Creating an object


Accessing Properties and Methods

 In PHP, we use the arrow operator (->) to access properties (variables)


and methods (functions) of an object.
 Accessing Properties
class Phone {
public $brand = "Samsung"; // Property
}

$myPhone = new Phone();


echo $myPhone->brand; // Output: Samsung
 Accessing Methods
class Phone {
public $brand = "Samsung";

public function call() { // Method


return "Calling...";
}
}

$myPhone = new Phone();


echo $myPhone->call(); // Output: Calling...
 Accessing Private Properties & Methods
 By default, only public properties and methods can be accessed.
 If a property or method is private, it cannot be accessed directly.
 We use getter and setter methods for private properties.
class BankAccount {
private $balance = 1000; // Private Property

public function getBalance() { // Getter Method


return $this->balance;
}
}

$account = new BankAccount();


// echo $account->balance; // ❌ Error: Cannot access private property
echo $account->getBalance(); // ✅ Output: 1000
 Modifying Properties
 We can update an object's property using ->
class Car {
public $color = "Red";
}

$myCar = new Car();


$myCar->color = "Blue"; // Changing the property value

echo $myCar->color; // Output: Blue


Declaring Class
 A class in PHP is like a blueprint for creating objects.
 It defines the properties (variables) and methods (functions) that objects of that
class will have.
class ClassName {
// Properties (variables)
public $property1;
public $property2;

// Methods (functions)
public function method1() {
// Code for method
}

public function method2() {


// Code for method
}
}
 Example of declaring a class:
class Car {
public $brand; // Property
public $color;

// Method to display car details


public function showCar() {
return "This car is a $this->color $this->brand.";
}
}
$brand and $color are properties.
showCar() is a method that displays car details.
 Creating an Object from a Class
 After defining a class, we create objects from it.
$myCar = new Car(); // Creating an object
$myCar->brand = "Toyota"; // Assigning values
$myCar->color = "Red";

echo $myCar->showCar(); // Output: This car is a Red Toyota.


$myCar is an object of class Car.
-> is used to access properties and methods of the object.
 Using a Constructor in a Class
 A constructor is a special method (__construct()) that runs automatically when an object is
created.
class Laptop {
public $brand;
public $price;
// Constructor
public function __construct($brand, $price) {
$this->brand = $brand;
$this->price = $price;
}
public function showLaptop() {
return "This laptop is a $this->brand and costs $this->price.";
}
}
$myLaptop = new Laptop("Dell", 1200);
echo $myLaptop->showLaptop();
// Output: This laptop is a Dell and costs $1200.
__construct($brand, $price) automatically assigns values to properties.
$this->property refers to the current object’s property.
Anonymous Class
 An anonymous class in PHP is a class without a name, which can be
created and used on the fly.
 These are useful when you need a class only once and don’t want to define a
full class separately.
 In PHP, anonymous classes are mainly used for:
✔ Quick object creation without defining a named class.
✔ Reducing unnecessary code when a class is needed only once.
✔ Improving readability in cases where a class is used locally.
$object = new class {
public function sayHello() {
return "Hello from an anonymous class!";
}
};

echo $object->sayHello();
// Output: Hello from an anonymous class!
 Anonymous Class with Constructor
 Anonymous classes can also have constructors just like regular classes.
$car = new class("Toyota") {
public $brand;

public function __construct($brand) {


$this->brand = $brand;
}

public function getBrand() {


return "This car is a " . $this->brand;
}
};

echo $car->getBrand();
// Output: This car is a Toyota
Constructor assigns the car's brand dynamically.
Method getBrand() returns the brand.
 Anonymous Class Extending a Regular Class
 An anonymous class can extend another class and override its methods.
class Greeting {
public function sayHi() {
return "Hi from the parent class!";
}
}

$greet = new class extends Greeting {


public function sayHi() {
return "Hello from the anonymous class!";
}
};

echo $greet->sayHi();
// Output: Hello from the anonymous class!

The anonymous class extends Greeting and overrides sayHi().


Examining Class and Object
 Examining a class or an object in PHP means checking its structure,
properties, methods, and parent class dynamically.
 PHP provides several built-in functions to examine classes and objects.
class Car {
public $brand = "Toyota";
public $color = "Red";
public function drive() {
return "The car is driving.";
}
}
$myCar = new Car();
// Get class name
echo "Class Name: " . get_class($myCar) . "<br>";
// Get class properties
print_r(get_class_vars("Car"));
echo "<br>";
// Get class methods
print_r(get_class_methods("Car"));
echo "<br>";
// Check if property exists
echo property_exists($myCar, "brand") ? "Property exists<br>" : "Property does not
exist<br>";
// Check if method exists
echo method_exists($myCar, "drive") ? "Method exists<br>" : "Method does not exist<br>";
 Output
Class Name: Car
Array ( [brand] => Toyota [color] => Red )
Array ( [0] => drive )
Property exists
Method exists

🔹 get_class($myCar) → Returns "Car"


🔹 get_class_vars("Car") → Lists class properties
🔹 get_class_methods("Car") → Lists class methods
🔹 property_exists($myCar, "brand") → Checks if "brand" exists
🔹 method_exists($myCar, "drive") → Checks if "drive" method exists
 Checking Parent Class (For Inheritance)
 If a class extends another class, we can check its parent.
class Vehicle {
public $type = "Car";
}

class Truck extends Vehicle {


public $capacity = "5 Tons";
}

$myTruck = new Truck();

// Get parent class


echo "Parent Class: " . get_parent_class($myTruck) . "<br>";
Output:
Parent Class: Vehicle
🔹 get_parent_class($myTruck) → Returns "Vehicle"
 Checking Object Type
$check = is_a($myTruck, "Truck") ? "Yes" : "No";
echo "Is myTruck an instance of Truck? " . $check;

Output:
Is myTruck an instance of Truck? Yes
🔹 is_a($obj, "ClassName") → Checks if the object belongs to the specified class.

You might also like