PHP Core / OOPs / Related and unrelated classess
Related and unrelated classes
-
Note
1. Related classes
A related class shares a logical connection or hierarchical connection with another class through inheritance or interface or composition
1. Inheritance Example
2. Interface exampleclass Animal { public function makeSound() { echo "Some sound"; } } class Dog extends Animal { public function makeSound() { echo "Bark"; } }
3. Composition exmpleinterface Logger { public function log($message); } class FileLogger implements Logger { public function log($message) { echo "Logging to file: $message"; } } class Engine { public function start() { echo "Engine started"; } } class Car { private $engine; public function __construct() { $this->engine = new Engine(); } public function startCar() { $this->engine->start(); } } 2. Unrelated Classes
An unrelated class has no direct relationship — it does not extend from same class or implement from the same interface.
class Car { public function startEngine() { echo "Engine started"; } } class Fruit { public function peel() { echo "Fruit peeled"; } }
MANVIA BLOG