PHP OOP Traits

🧩 PHP OOP – Traits

https://www.cs.sjsu.edu/~pearce/modules/lectures/scala/oop/Traits_files/image002.jpg

Traits allow you to reuse code across multiple classes.
They solve a major limitation in PHP:

PHP does NOT support multiple inheritance
(a class cannot extend more than one parent class)

Traits allow us to β€œmix in” methods from multiple places.


🟦 1️⃣ What is a Trait?

A trait is like a mini-class containing methods (and properties) that can be reused in multiple classes.

Syntax:

trait MyTrait {
public function sayHello() {
echo "Hello!";
}
}

Use a trait in a class:

class MyClass {
use MyTrait;
}

$obj = new MyClass();
$obj->sayHello();


🟩 2️⃣ Using Multiple Traits in One Class

trait A {
public function methodA() {
echo "A";
}
}

trait B {
public function methodB() {
echo "B";
}
}

class Test {
use A, B;
}

$t = new Test();
$t->methodA();
$t->methodB();


🟧 3️⃣ Traits with Properties

trait Info {
public $name = "Default Name";
}

Use inside a class:

class Person {
use Info;
}

$p = new Person();
echo $p->name;


πŸŸ₯ 4️⃣ Traits with Same Method Names (Conflict Resolution)

If two traits have same method name β†’ PHP throws conflict.
We solve it with insteadof and as.

Example:

trait A {
public function show() {
echo "A";
}
}

trait B {
public function show() {
echo "B";
}
}

class Test {
use A, B {
A::show insteadof B;
B::show as showB; // alias
}
}

$t = new Test();
$t->show(); // A
$t->showB(); // B


🟨 5️⃣ Trait + Class Inheritance Together

trait Logger {
public function log() {
echo "Logging...";
}
}

class ParentClass {
public function show() {
echo "Parent class";
}
}

class ChildClass extends ParentClass {
use Logger;
}

$obj = new ChildClass();
$obj->show();
$obj->log();


🟫 6️⃣ Traits Inside Traits (Trait Composition)

trait A {
public function a() { echo "A"; }
}

trait B {
use A;
public function b() { echo "B"; }
}

class Test {
use B;
}

$t = new Test();
$t->a();
$t->b();


πŸŸͺ 7️⃣ Traits Cannot Be Instantiated

You cannot do:

$t = new MyTrait(); // ❌ ERROR

Traits are not classes, only reusable code blocks.


🟦 8️⃣ Practical Use Case – Logging System

trait Logger {
public function log($msg) {
echo "[LOG]: $msg";
}
}

class User {
use Logger;

public function create() {
$this->log("User created");
}
}

$u = new User();
$u->create();


🧠 Traits vs Interfaces vs Inheritance

Feature Trait Interface Inheritance
Method body βœ” Yes ❌ No (only declare) βœ” Yes
Properties βœ” Yes βœ” (constants only) βœ” Yes
Multiple βœ” Yes (multi-trait) βœ” Yes ❌ Only one parent
Purpose Reuse code Define rules Create structure

🎯 Summary

Concept Meaning
Trait Reusable set of methods/properties
use Include trait inside class
insteadof Resolve trait method conflict
as Alias trait method

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *