Skip to content

PRO TIP Block youtube.com at the DNS level — Pi-hole, NextDNS or your hosts file — but allow youtube-nocookie.com and i.ytimg.com. These tutorials keep playing; the rabbit hole does not.

Learning without distractions

PHP Traits – How They Work & Drawbacks – Full PHP 8 Tutorial

41.3K views on YouTube

In this video, you will learn all you need to know about traits in PHP with examples & in-depth explanations. You’ll learn what problems traits solve as well as the problems it can introduce.

SOME OF THE WAYS YOU CAN SUPPORT THE CHANNEL
👍 Smash the like button
🤝 Subscribe to the channel & turn the notifications on
💬 Post comments, any feedback is greatly appreciated
⭐ Become a Patreon: https://www.patreon.com/programwithgio
THANK YOU!

LESSON 2.14
Course Outline – https://github.com/ggelashvili/learnphptherightway-outline
Course Playlist – https://www.youtube.com/playlist?list=PLr3d3QYzkw2xabQRUpcZ_IBk9W50M9pe-

RESOURCES
https://www.php.net/manual/en/language.oop5.traits.php

CHAPTERS
00:00 – Intro
00:27 – Multiple inheritance problem
02:02 – Overview of coffee maker example in code
03:08 – Using interfaces instead of multiple inheritance
05:43 – Creating & using traits
08:22 – How do traits work?
08:39 – Method precedence
11:02 – Conflict resolution (“insteadof” & “as” operators)
13:38 – Changing visibility of trait methods
16:02 – Composing traits from other traits
16:46 – Properties within traits
20:40 – Abstract methods in traits
23:19 – Static properties & methods in traits
25:38 – Magic __CLASS__ constant in traits
26:13 – Personal opinion & downsides of traits
28:46 – Example comparing inheritance & traits