Object-Oriented PHP
Modern PHP development heavily relies on Object-Oriented Programming (OOP) principles.
Classes and Objects
<?php
class User {
public $name;
private $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function getEmail() {
return $this->email;
}
}
$user = new User("John Doe", "john@example.com");
echo $user->name; // John Doe
?>Key Concepts
- Encapsulation: Using public, private, and protected visibility
- Inheritance: Extending classes
- Polymorphism: Method overriding
- Traits: Code reusability
- Interfaces: Defining contracts