oops consept in php
- User
oops consept in php
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
oops consept in php
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure, here's an overview of the key Object-Oriented Programming (OOP) concepts in PHP:
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function displayInfo() {
return "This car is a " . $this->color . " " . $this->model;
}
}
$myCar = new Car('red', 'Sedan');
echo $myCar->displayInfo(); // Output: This car is a red Sedan
class Dog {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function bark() {
return $this->name . " says Woof!";
}
}
$myDog = new Dog('Buddy', 5);
echo $myDog->bark(); // Output: Buddy says Woof!
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function makeSound() {
return $this->name . " makes a sound.";
}
}
class Cat extends Animal {
public function meow() {
return $this->name . " says Meow!";
}
}
$myCat = new Cat('Whiskers');
echo $myCat->meow(); // Output: Whiskers says Meow!
echo $myCat->makeSound(); // Output: Whiskers makes a sound.
class BankAccount {
private $balance;
public function __construct($initialBalance) {
$this->balance = $initialBalance;
}
public function getBalance() {
return $this->balance;
}
public function deposit($amount) {
$this->balance += $amount;
}
public function withdraw($amount) {
if ($amount <= $this->balance) {
$this->balance -= $amount;
} else {
echo "Insufficient funds";
}
}
}
$account = new BankAccount(100);
$account->deposit(50);
echo $account->getBalance(); // Output: 150
$account->withdraw(30);
echo $account->getBalance(); // Output: 120
class Shape {
public function draw() {
return "Drawing a shape.";
}
}
class Circle extends Shape {
public function draw() {
return "Drawing a circle.";
}
}
class Square extends Shape {
public function draw() {
return "Drawing a square.";
}
}
function drawShape(Shape $shape) {
echo $shape->draw();
}
$circle = new Circle();
$square = new Square();
drawShape($circle); // Output: Drawing a circle.
drawShape($square); // Output: Drawing a square.
abstract class Vehicle {
abstract public function start();
public function stop() {
return "Vehicle stopped.";
}
}
class Car extends Vehicle {
public function start() {
return "Car started.";
}
}
$car = new Car();
echo $car->start(); // Output: Car started.
echo $car->stop(); // Output: Vehicle stopped.
// Interface example
interface AnimalInterface {
public function makeSound();
}
class Dog implements AnimalInterface {
public function makeSound() {
return "Woof!";
}
}
$dog = new Dog();
echo $dog->makeSound(); // Output: Woof!
trait Logger {
public function log($message) {
echo "Logging message: " . $message;
}
}
class User {
use Logger;
public function register() {
$this->log("User registered");
}
}
$user = new User();
$user->register(); // Output: Logging message: User registered
These concepts form the core of OOP in PHP and help in creating more structured, maintainable, and reusable code.