0% found this document useful (0 votes)
4 views3 pages

Chapter 4

Uploaded by

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

Chapter 4

Uploaded by

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

Absolutely!

Let’s simplify **Chapter 4: Classes and Objects in PHP** into easy-to-


understand parts with examples and explanations.

---

### 🔹 1. **What is a Class?**

Think of a class like a **blueprint** for creating things. For example, a blueprint
for a "Car."

```php
<?php
class Car {
public $color;
public $brand;

public function honk() {


echo "Beep beep!";
}
}
?>
```

🧠 **Key Idea**:
A class defines **what properties and actions** an object should have.

---

### 🔹 2. **What is an Object?**

An object is the **actual car** made using the blueprint.

```php
<?php
$myCar = new Car(); // Create a Car object
$myCar->color = "Red"; // Set property
$myCar->honk(); // Call method → Output: Beep beep!
?>
```

🧠 **Key Idea**:
An object is a **real example** of a class.

---

### 🔹 3. **Constructor**

Used to **automatically set values** when the object is created.

```php
<?php
class Car {
public $brand;

public function __construct($brand) {


$this->brand = $brand;
}
}
$car1 = new Car("Toyota");
echo $car1->brand; // Output: Toyota
?>
```

🧠 **\$this** refers to **the current object**.

---

### 🔹 4. **Destructor**

Runs when the object is destroyed (usually at the end).

```php
<?php
class Goodbye {
public function __destruct() {
echo "Object is destroyed.";
}
}
$bye = new Goodbye(); // Output when program ends: Object is destroyed.
?>
```

---

### 🔹 5. **Accessing Properties & Methods**

Use `->` to get or set properties and call methods.

```php
$car1->brand = "Honda"; // Set property
echo $car1->brand; // Get property
$car1->honk(); // Call method
```

---

### 🔹 6. **Encapsulation (Visibility)**

Use `public`, `private`, or `protected` to control access.

```php
class Person {
private $name;

public function setName($name) {


$this->name = $name;
}

public function getName() {


return $this->name;
}
}
```

---

### 🔹 7. **Inheritance**
A class can **reuse** another class.

```php
class Animal {
public function makeSound() {
echo "Animal sound";
}
}

class Dog extends Animal {


public function bark() {
echo "Woof!";
}
}

$dog = new Dog();


$dog->makeSound(); // Inherited
$dog->bark(); // Own method
```

---

### 🔹 8. **Overloading with Magic Methods**

Used to handle undefined properties or methods.

```php
class Demo {
public function __get($name) {
echo "Trying to access $name";
}
}

$obj = new Demo();


echo $obj->unknown; // Output: Trying to access unknown
```

---

### 🔹 9. **Form Handling**

**HTML Form:**

```html
<form action="process.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>
```

**PHP File (process.php):**

```php
<?php
$name = $_POST['name'];
echo "Hello, " . $name;
?>
```

You might also like