{"id":406,"date":"2025-05-19T12:41:46","date_gmt":"2025-05-19T04:41:46","guid":{"rendered":"http:\/\/dangnh.cf\/?p=406"},"modified":"2025-05-19T13:02:04","modified_gmt":"2025-05-19T05:02:04","slug":"trait-mixin-in-php-part-2","status":"publish","type":"post","link":"https:\/\/dangnh.cf\/vi\/2025\/05\/19\/trait-mixin-in-php-part-2\/","title":{"rendered":"Trait \u2013 Mixin in PHP? &#8211; PART 2"},"content":{"rendered":"<p>D\u01b0\u1edbi \u0111\u00e2y l\u00e0 ph\u1ea7n 2 (v\u00e0 c\u0169ng l\u00e0 ph\u1ea7n cu\u1ed1i) c\u1ee7a series \u201cTrait \u2013 Mixin trong PHP\u201d. \u1ede ph\u1ea7n n\u00e0y ch\u00fang ta s\u1ebd \u0111i s\u00e2u v\u00e0o c\u00e1ch x\u1eed l\u00fd xung \u0111\u1ed9t, m\u1edf r\u1ed9ng kh\u1ea3 n\u0103ng c\u1ee7a Trait v\u00e0 c\u00e1c v\u00ed d\u1ee5 th\u1ef1c ti\u1ec5n.<br \/>\nM\u1eddi c\u00e1c b\u1ea1n xem l\u1ea1i ph\u1ea7n 1 t\u1ea1i \u0111\u00e2y: <a href=\"https:\/\/dangnh.cf\/vi\/2018\/06\/01\/trait-mixin-in-php\/\">https:\/\/dangnh.cf\/2018\/06\/01\/trait-mixin-in-php\/<\/a><\/p>\n<h2><b>1. Gi\u1ea3i quy\u1ebft xung \u0111\u1ed9t gi\u1eefa c\u00e1c Trait<\/b><\/h2>\n<p class=\"p1\">Khi m\u1ed9t class <span class=\"s2\">use<\/span> nhi\u1ec1u trait v\u00e0 c\u00f3 ph\u01b0\u01a1ng th\u1ee9c tr\u00f9ng t\u00ean, PHP s\u1ebd b\u00e1o l\u1ed7i. \u0110\u1ec3 tr\u00e1nh, ta d\u00f9ng hai t\u1eeb kh\u00f3a:<\/p>\n<h3><b>1.1. <\/b><b>insteadof<\/b><\/h3>\n<p class=\"p1\">Ch\u1ecdn ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a trait n\u00e0y thay v\u00ec c\u1ee7a trait kia:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ntrait A {\r\n    public function foo() { echo &quot;A::foo\\n&quot;; }\r\n}\r\ntrait B {\r\n    public function foo() { echo &quot;B::foo\\n&quot;; }\r\n}\r\n\r\nclass MyClass {\r\n    use A, B {\r\n        A::foo insteadof B; \/\/ \u01b0u ti\u00ean A::foo\r\n    }\r\n}\r\n\r\n(new MyClass)-&amp;gt;foo(); \/\/ In ra &quot;A::foo&quot;\r\n<\/pre>\n<h3><b>1.2. <\/b><b>as<\/b><\/h3>\n<p class=\"p3\">\u0110\u1ed5i t\u00ean (alias) cho ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ec3 v\u1eabn c\u00f3 th\u1ec3 g\u1ecdi c\u1ea3 hai:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nclass MyClass {\r\n    use A, B {\r\n        A::foo insteadof B;\r\n        B::foo as bar;   \/\/ g\u1ecdi B::foo qua t\u00ean m\u1edbi bar()\r\n    }\r\n}\r\n\r\n$obj = new MyClass;\r\n$obj-&amp;gt;foo(); \/\/ A::foo\r\n$obj-&amp;gt;bar(); \/\/ B::foo\r\n<\/pre>\n<h2><b>2. C\u00e1c t\u00ednh n\u0103ng n\u00e2ng cao c\u1ee7a Trait<\/b><\/h2>\n<h3><b>2.1. Abstract method trong Trait<\/b><\/h3>\n<p class=\"p3\">B\u1ea1n c\u00f3 th\u1ec3 khai b\u00e1o ph\u01b0\u01a1ng th\u1ee9c <span class=\"s2\">abstract<\/span> trong trait, bu\u1ed9c class k\u1ebf th\u1eeba ph\u1ea3i tri\u1ec3n khai:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ntrait LoggerTrait {\r\n    abstract protected function getLogFile(): string;\r\n\r\n    public function log($msg) {\r\n        file_put_contents($this-&amp;gt;getLogFile(), $msg.&quot;\\n&quot;, FILE_APPEND);\r\n    }\r\n}\r\n\r\nclass App {\r\n    use LoggerTrait;\r\n\r\n    protected function getLogFile(): string {\r\n        return __DIR__ . '\/app.log';\r\n    }\r\n}<\/pre>\n<h3><b>2.2. Trait v\u1edbi thu\u1ed9c t\u00ednh <\/b><b>static v\u00e0\u00a0<\/b><b>const<\/b><\/h3>\n<p class=\"p3\">Trait cho ph\u00e9p \u0111\u1ecbnh ngh\u0129a <span class=\"s2\">static<\/span> property v\u00e0 <span class=\"s2\">const<\/span>:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ntrait Configurable {\r\n    protected static array $options = &#x5B;];\r\n\r\n    public static function setOption($k, $v) {\r\n        static::$options&#x5B;$k] = $v;\r\n    }\r\n    public static function getOption($k) {\r\n        return static::$options&#x5B;$k] ?? null;\r\n    }\r\n}<\/pre>\n<h3><b>2.3. Thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c private\/protected<\/b><\/h3>\n<p class=\"p3\">Trait c\u00f3 th\u1ec3 khai b\u00e1o method <span class=\"s2\">private<\/span> hay <span class=\"s2\">protected<\/span>, ch\u1ec9 class ch\u1ee9a trait bi\u1ebft d\u00f9ng:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ntrait SecretTrait {\r\n    private function secret() {\r\n        return &quot;Ch\u1ec9 c\u00f3 t\u00f4i m\u1edbi bi\u1ebft&quot;;\r\n    }\r\n    public function reveal() {\r\n        return $this-&amp;gt;secret();\r\n    }\r\n}<\/pre>\n<h3><b>2.4. Trait th\u1eeba k\u1ebf Trait<\/b><\/h3>\n<p class=\"p3\">Trait c\u0169ng c\u00f3 th\u1ec3 <span class=\"s2\">use<\/span> trait kh\u00e1c:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ntrait A { \/* ... *\/ }\r\ntrait B { use A; \/* ... *\/ }\r\nclass C {\r\n    use B; \/\/ s\u1ebd bao g\u1ed3m c\u1ea3 A\r\n}<\/pre>\n<h2><b>3. V\u00ed d\u1ee5 th\u1ef1c t\u1ebf<\/b><\/h2>\n<h3><b>3.1. Trait \u201cSoftDelete\u201d cho Model<\/b><\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ntrait SoftDelete {\r\n    protected bool $deleted = false;\r\n\r\n    public function delete() {\r\n        $this-&amp;gt;deleted = true;\r\n    }\r\n    public function restore() {\r\n        $this-&amp;gt;deleted = false;\r\n    }\r\n    public function isDeleted(): bool {\r\n        return $this-&amp;gt;deleted;\r\n    }\r\n}\r\n\r\nclass User {\r\n    use SoftDelete;\r\n    \/\/ ...\r\n}<\/pre>\n<p class=\"p1\">Gi\u1edd ch\u1ec9 c\u1ea7n <span class=\"s1\">use SoftDelete<\/span>, m\u1ecdi class \u0111\u1ec1u c\u00f3 t\u00ednh n\u0103ng x\u00f3a m\u1ec1m.<\/p>\n<h3><b>3.2. Trait \u201cTimestampable\u201d<\/b><\/h3>\n<p class=\"p4\"><span class=\"s3\">T\u1ef1 \u0111\u1ed9ng qu\u1ea3n l\u00fd <\/span>created_at<span class=\"s3\">\/<\/span>updated_at<span class=\"s3\">:<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ntrait Timestampable {\r\n    protected \\DateTime $createdAt;\r\n    protected \\DateTime $updatedAt;\r\n\r\n    public function touch() {\r\n        $this-&amp;gt;updatedAt = new \\DateTime;\r\n    }\r\n    public function initTimestamps() {\r\n        $this-&amp;gt;createdAt = $this-&amp;gt;updatedAt = new \\DateTime;\r\n    }\r\n}\r\n\r\nclass Post {\r\n    use Timestampable;\r\n\r\n    public function __construct() {\r\n        $this-&amp;gt;initTimestamps();\r\n    }\r\n}<\/pre>\n<h2><b>4. Best Practices khi d\u00f9ng Trait<\/b><\/h2>\n<ul>\n<li>\n<p class=\"p1\"><span class=\"s1\"><b>Gi\u1eef trait nh\u1ecf, chuy\u00ean bi\u1ec7t<\/b><\/span>: M\u1ed7i trait ch\u1ec9 \u0111\u1ea3m nh\u1eadn m\u1ed9t nhi\u1ec7m v\u1ee5 duy nh\u1ea5t.<\/p>\n<\/li>\n<li>\n<p class=\"p1\"><span class=\"s1\"><b>\u0110\u1eb7t t\u00ean r\u00f5 r\u00e0ng<\/b><\/span><span class=\"s2\">: V\u00ed d\u1ee5 <\/span>Loggable<span class=\"s2\">, <\/span>Notifiable<span class=\"s2\">, <\/span>SoftDeleteable<span class=\"s2\">\u2026<\/span><\/p>\n<\/li>\n<li>\n<p class=\"p1\"><span class=\"s1\"><b>Tr\u00e1nh trait qu\u00e1 \u201cn\u1eb7ng\u201d<\/b><\/span>: N\u1ebfu trait ch\u1ee9a qu\u00e1 nhi\u1ec1u logic, c\u00e2n nh\u1eafc t\u00e1ch th\u00e0nh class helper ho\u1eb7c service.<\/p>\n<\/li>\n<li>\n<p class=\"p1\"><span class=\"s1\"><b>T\u00e0i li\u1ec7u \u0111\u1ea7y \u0111\u1ee7<\/b><\/span>: Khai b\u00e1o <span class=\"s2\">@method<\/span> hay <span class=\"s2\">@property<\/span> trong DocBlock n\u1ebfu trait \u201cinject\u201d th\u00eam method hay property cho IDE.<\/p>\n<\/li>\n<\/ul>\n<h2><b>5. K\u1ebft lu\u1eadn<\/b><\/h2>\n<p class=\"p3\">Trait l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap t\u00e1i s\u1eed d\u1ee5ng code v\u00e0 \u201cmixin\u201d behavior v\u00e0o nhi\u1ec1u class kh\u00e1c nhau m\u00e0 kh\u00f4ng c\u1ea7n \u0111a k\u1ebf th\u1eeba. Tuy nhi\u00ean, l\u1ea1m d\u1ee5ng qu\u00e1 nhi\u1ec1u trait c\u00f3 th\u1ec3 l\u00e0m code r\u1ed1i v\u00e0 kh\u00f3 theo d\u00f5i. H\u00e3y thi\u1ebft k\u1ebf trait g\u1ecdn, r\u00f5 r\u00e0ng, v\u00e0 \u00e1p d\u1ee5ng cho nh\u1eefng h\u00e0m th\u1ef1c s\u1ef1 chung nh\u1ea5t.<\/p>\n<p class=\"p3\">C\u1ea3m \u01a1n b\u1ea1n \u0111\u00e3 theo d\u00f5i series v\u1ec1 Trait trong PHP. Ch\u00fac c\u00e1c b\u1ea1n \u00e1p d\u1ee5ng hi\u1ec7u qu\u1ea3 v\u00e0 happy coding!<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>T\u00e0i li\u1ec7u li\u00ean quan:<\/p>\n<ul>\n<li>\n<p class=\"p1\">PHP Manual \u2013 Traits: <a href=\"https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php\">https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php<\/a><span class=\"Apple-converted-space\">\u00a0 <\/span><\/p>\n<\/li>\n<li>\n<p class=\"p1\">PHP Manual \u2013 Conflict Resolution: <a href=\"https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php#traits.conflict.resolution\">https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php#traits.conflict.resolution<\/a><\/p>\n<\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>D\u01b0\u1edbi \u0111\u00e2y l\u00e0 ph\u1ea7n 2 (v\u00e0 c\u0169ng l\u00e0 ph\u1ea7n cu\u1ed1i) c\u1ee7a series \u201cTrait \u2013 Mixin trong PHP\u201d. \u1ede ph\u1ea7n n\u00e0y ch\u00fang ta s\u1ebd \u0111i s\u00e2u v\u00e0o c\u00e1ch x\u1eed l\u00fd xung \u0111\u1ed9t, m\u1edf r\u1ed9ng kh\u1ea3 n\u0103ng c\u1ee7a Trait v\u00e0 c\u00e1c v\u00ed d\u1ee5 th\u1ef1c ti\u1ec5n. M\u1eddi c\u00e1c b\u1ea1n xem l\u1ea1i ph\u1ea7n 1 t\u1ea1i \u0111\u00e2y: https:\/\/dangnh.cf\/2018\/06\/01\/trait-mixin-in-php\/ 1. Gi\u1ea3i quy\u1ebft xung \u0111\u1ed9t gi\u1eefa c\u00e1c Trait Khi m\u1ed9t class use nhi\u1ec1u trait v\u00e0 c\u00f3 ph\u01b0\u01a1ng th\u1ee9c tr\u00f9ng t\u00ean, PHP s\u1ebd b\u00e1o l\u1ed7i. \u0110\u1ec3 tr\u00e1nh, ta d\u00f9ng hai t\u1eeb kh\u00f3a: 1.1. insteadof Ch\u1ecdn ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a trait n\u00e0y thay v\u00ec c\u1ee7a trait kia: trait A { public function foo() { echo &quot;A::foo\\n&quot;; } } trait B { public function foo() { echo &quot;B::foo\\n&quot;; } } class MyClass { use A, B { A::foo insteadof B; \/\/ \u01b0u ti\u00ean A::foo } } (new MyClass)-&amp;gt;foo(); \/\/ In ra &quot;A::foo&quot; 1.2. as \u0110\u1ed5i t\u00ean (alias) cho ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ec3 v\u1eabn c\u00f3 th\u1ec3 g\u1ecdi c\u1ea3 hai: class MyClass { use A, B { A::foo insteadof B; B::foo as bar; \/\/ g\u1ecdi B::foo qua t\u00ean m\u1edbi bar() } } $obj = new MyClass; $obj-&amp;gt;foo(); \/\/ A::foo $obj-&amp;gt;bar(); \/\/ B::foo 2. C\u00e1c t\u00ednh n\u0103ng n\u00e2ng cao c\u1ee7a Trait 2.1. Abstract method trong Trait B\u1ea1n c\u00f3 th\u1ec3 khai b\u00e1o ph\u01b0\u01a1ng th\u1ee9c abstract trong trait, bu\u1ed9c class k\u1ebf th\u1eeba ph\u1ea3i tri\u1ec3n khai: trait LoggerTrait { abstract protected function getLogFile(): string; public function log($msg) { file_put_contents($this-&amp;gt;getLogFile(), $msg.&quot;\\n&quot;, FILE_APPEND); } } class App { use LoggerTrait; protected function getLogFile(): string { return __DIR__ . &#8216;\/app.log&#8217;; } } 2.2. Trait v\u1edbi thu\u1ed9c t\u00ednh static v\u00e0\u00a0const Trait cho ph\u00e9p \u0111\u1ecbnh ngh\u0129a static property v\u00e0 const: trait Configurable { protected static array $options = &#x5B;]; public static function setOption($k, $v) { static::$options&#x5B;$k] = $v; } public static function getOption($k) { return static::$options&#x5B;$k] ?? null; } } 2.3. Thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c private\/protected Trait c\u00f3 th\u1ec3 khai b\u00e1o method private hay protected, ch\u1ec9 class ch\u1ee9a trait bi\u1ebft d\u00f9ng: trait SecretTrait { private function secret() { return &quot;Ch\u1ec9 c\u00f3 t\u00f4i m\u1edbi bi\u1ebft&quot;; } public function reveal() { return $this-&amp;gt;secret(); } } 2.4. Trait th\u1eeba k\u1ebf Trait Trait c\u0169ng c\u00f3 th\u1ec3 use trait kh\u00e1c: trait A { \/* &#8230; *\/ } trait B { use A; \/* &#8230; *\/ } class C { use B; \/\/ s\u1ebd bao g\u1ed3m c\u1ea3 A } 3. V\u00ed d\u1ee5 th\u1ef1c t\u1ebf 3.1. Trait \u201cSoftDelete\u201d cho Model trait SoftDelete { protected bool $deleted = false; public function delete() { $this-&amp;gt;deleted = true; } public function restore() { $this-&amp;gt;deleted = false; } public function isDeleted(): bool { return $this-&amp;gt;deleted; } } class User { use SoftDelete; \/\/ &#8230; } Gi\u1edd ch\u1ec9 c\u1ea7n use SoftDelete, m\u1ecdi class \u0111\u1ec1u c\u00f3 t\u00ednh n\u0103ng x\u00f3a m\u1ec1m. 3.2. Trait \u201cTimestampable\u201d T\u1ef1 \u0111\u1ed9ng qu\u1ea3n l\u00fd created_at\/updated_at: trait Timestampable { protected \\DateTime $createdAt; protected \\DateTime $updatedAt; public function touch() { $this-&amp;gt;updatedAt = new \\DateTime; } public function initTimestamps() { $this-&amp;gt;createdAt = $this-&amp;gt;updatedAt = new \\DateTime; } } class Post { use Timestampable; public function __construct() { $this-&amp;gt;initTimestamps(); } } 4. Best Practices khi d\u00f9ng Trait Gi\u1eef trait nh\u1ecf, chuy\u00ean bi\u1ec7t: M\u1ed7i trait ch\u1ec9 \u0111\u1ea3m nh\u1eadn m\u1ed9t nhi\u1ec7m v\u1ee5 duy nh\u1ea5t. \u0110\u1eb7t t\u00ean r\u00f5 r\u00e0ng: V\u00ed d\u1ee5 Loggable, Notifiable, SoftDeleteable\u2026 Tr\u00e1nh trait qu\u00e1 \u201cn\u1eb7ng\u201d: N\u1ebfu trait ch\u1ee9a qu\u00e1 nhi\u1ec1u logic, c\u00e2n nh\u1eafc t\u00e1ch th\u00e0nh class helper ho\u1eb7c service. T\u00e0i li\u1ec7u \u0111\u1ea7y \u0111\u1ee7: Khai b\u00e1o @method hay @property trong DocBlock n\u1ebfu trait \u201cinject\u201d th\u00eam method hay property cho IDE. 5. K\u1ebft lu\u1eadn Trait l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap t\u00e1i s\u1eed d\u1ee5ng code v\u00e0 \u201cmixin\u201d behavior v\u00e0o nhi\u1ec1u class kh\u00e1c nhau m\u00e0 kh\u00f4ng c\u1ea7n \u0111a k\u1ebf th\u1eeba. Tuy nhi\u00ean, l\u1ea1m d\u1ee5ng qu\u00e1 nhi\u1ec1u trait c\u00f3 th\u1ec3 l\u00e0m code r\u1ed1i v\u00e0 kh\u00f3 theo d\u00f5i. H\u00e3y thi\u1ebft k\u1ebf trait g\u1ecdn, r\u00f5 r\u00e0ng, v\u00e0 \u00e1p d\u1ee5ng cho nh\u1eefng h\u00e0m th\u1ef1c s\u1ef1 chung nh\u1ea5t. C\u1ea3m \u01a1n b\u1ea1n \u0111\u00e3 theo d\u00f5i series v\u1ec1 Trait trong PHP. Ch\u00fac c\u00e1c b\u1ea1n \u00e1p d\u1ee5ng hi\u1ec7u qu\u1ea3 v\u00e0 happy coding! &nbsp; T\u00e0i li\u1ec7u li\u00ean quan: PHP Manual \u2013 Traits: https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php\u00a0 PHP Manual \u2013 Conflict Resolution: https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php#traits.conflict.resolution<\/p>","protected":false},"author":1,"featured_media":349,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[15,17],"class_list":["post-406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-php","tag-trait"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Trait \u2013 Mixin in PHP? - PART 2 - Mark&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/dangnh.cf\/vi\/2025\/05\/19\/trait-mixin-in-php-part-2\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Trait \u2013 Mixin in PHP? - PART 2 - Mark&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"D\u01b0\u1edbi \u0111\u00e2y l\u00e0 ph\u1ea7n 2 (v\u00e0 c\u0169ng l\u00e0 ph\u1ea7n cu\u1ed1i) c\u1ee7a series \u201cTrait \u2013 Mixin trong PHP\u201d. \u1ede ph\u1ea7n n\u00e0y ch\u00fang ta s\u1ebd \u0111i s\u00e2u v\u00e0o c\u00e1ch x\u1eed l\u00fd xung \u0111\u1ed9t, m\u1edf r\u1ed9ng kh\u1ea3 n\u0103ng c\u1ee7a Trait v\u00e0 c\u00e1c v\u00ed d\u1ee5 th\u1ef1c ti\u1ec5n. M\u1eddi c\u00e1c b\u1ea1n xem l\u1ea1i ph\u1ea7n 1 t\u1ea1i \u0111\u00e2y: https:\/\/dangnh.cf\/2018\/06\/01\/trait-mixin-in-php\/ 1. Gi\u1ea3i quy\u1ebft xung \u0111\u1ed9t gi\u1eefa c\u00e1c Trait Khi m\u1ed9t class use nhi\u1ec1u trait v\u00e0 c\u00f3 ph\u01b0\u01a1ng th\u1ee9c tr\u00f9ng t\u00ean, PHP s\u1ebd b\u00e1o l\u1ed7i. \u0110\u1ec3 tr\u00e1nh, ta d\u00f9ng hai t\u1eeb kh\u00f3a: 1.1. insteadof Ch\u1ecdn ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a trait n\u00e0y thay v\u00ec c\u1ee7a trait kia: trait A { public function foo() { echo &quot;A::foon&quot;; } } trait B { public function foo() { echo &quot;B::foon&quot;; } } class MyClass { use A, B { A::foo insteadof B; \/\/ \u01b0u ti\u00ean A::foo } } (new MyClass)-&amp;gt;foo(); \/\/ In ra &quot;A::foo&quot; 1.2. as \u0110\u1ed5i t\u00ean (alias) cho ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ec3 v\u1eabn c\u00f3 th\u1ec3 g\u1ecdi c\u1ea3 hai: class MyClass { use A, B { A::foo insteadof B; B::foo as bar; \/\/ g\u1ecdi B::foo qua t\u00ean m\u1edbi bar() } } $obj = new MyClass; $obj-&amp;gt;foo(); \/\/ A::foo $obj-&amp;gt;bar(); \/\/ B::foo 2. C\u00e1c t\u00ednh n\u0103ng n\u00e2ng cao c\u1ee7a Trait 2.1. Abstract method trong Trait B\u1ea1n c\u00f3 th\u1ec3 khai b\u00e1o ph\u01b0\u01a1ng th\u1ee9c abstract trong trait, bu\u1ed9c class k\u1ebf th\u1eeba ph\u1ea3i tri\u1ec3n khai: trait LoggerTrait { abstract protected function getLogFile(): string; public function log($msg) { file_put_contents($this-&amp;gt;getLogFile(), $msg.&quot;n&quot;, FILE_APPEND); } } class App { use LoggerTrait; protected function getLogFile(): string { return __DIR__ . &#039;\/app.log&#039;; } } 2.2. Trait v\u1edbi thu\u1ed9c t\u00ednh static v\u00e0\u00a0const Trait cho ph\u00e9p \u0111\u1ecbnh ngh\u0129a static property v\u00e0 const: trait Configurable { protected static array $options = &#x5B;]; public static function setOption($k, $v) { static::$options&#x5B;$k] = $v; } public static function getOption($k) { return static::$options&#x5B;$k] ?? null; } } 2.3. Thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c private\/protected Trait c\u00f3 th\u1ec3 khai b\u00e1o method private hay protected, ch\u1ec9 class ch\u1ee9a trait bi\u1ebft d\u00f9ng: trait SecretTrait { private function secret() { return &quot;Ch\u1ec9 c\u00f3 t\u00f4i m\u1edbi bi\u1ebft&quot;; } public function reveal() { return $this-&amp;gt;secret(); } } 2.4. Trait th\u1eeba k\u1ebf Trait Trait c\u0169ng c\u00f3 th\u1ec3 use trait kh\u00e1c: trait A { \/* ... *\/ } trait B { use A; \/* ... *\/ } class C { use B; \/\/ s\u1ebd bao g\u1ed3m c\u1ea3 A } 3. V\u00ed d\u1ee5 th\u1ef1c t\u1ebf 3.1. Trait \u201cSoftDelete\u201d cho Model trait SoftDelete { protected bool $deleted = false; public function delete() { $this-&amp;gt;deleted = true; } public function restore() { $this-&amp;gt;deleted = false; } public function isDeleted(): bool { return $this-&amp;gt;deleted; } } class User { use SoftDelete; \/\/ ... } Gi\u1edd ch\u1ec9 c\u1ea7n use SoftDelete, m\u1ecdi class \u0111\u1ec1u c\u00f3 t\u00ednh n\u0103ng x\u00f3a m\u1ec1m. 3.2. Trait \u201cTimestampable\u201d T\u1ef1 \u0111\u1ed9ng qu\u1ea3n l\u00fd created_at\/updated_at: trait Timestampable { protected DateTime $createdAt; protected DateTime $updatedAt; public function touch() { $this-&amp;gt;updatedAt = new DateTime; } public function initTimestamps() { $this-&amp;gt;createdAt = $this-&amp;gt;updatedAt = new DateTime; } } class Post { use Timestampable; public function __construct() { $this-&amp;gt;initTimestamps(); } } 4. Best Practices khi d\u00f9ng Trait Gi\u1eef trait nh\u1ecf, chuy\u00ean bi\u1ec7t: M\u1ed7i trait ch\u1ec9 \u0111\u1ea3m nh\u1eadn m\u1ed9t nhi\u1ec7m v\u1ee5 duy nh\u1ea5t. \u0110\u1eb7t t\u00ean r\u00f5 r\u00e0ng: V\u00ed d\u1ee5 Loggable, Notifiable, SoftDeleteable\u2026 Tr\u00e1nh trait qu\u00e1 \u201cn\u1eb7ng\u201d: N\u1ebfu trait ch\u1ee9a qu\u00e1 nhi\u1ec1u logic, c\u00e2n nh\u1eafc t\u00e1ch th\u00e0nh class helper ho\u1eb7c service. T\u00e0i li\u1ec7u \u0111\u1ea7y \u0111\u1ee7: Khai b\u00e1o @method hay @property trong DocBlock n\u1ebfu trait \u201cinject\u201d th\u00eam method hay property cho IDE. 5. K\u1ebft lu\u1eadn Trait l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap t\u00e1i s\u1eed d\u1ee5ng code v\u00e0 \u201cmixin\u201d behavior v\u00e0o nhi\u1ec1u class kh\u00e1c nhau m\u00e0 kh\u00f4ng c\u1ea7n \u0111a k\u1ebf th\u1eeba. Tuy nhi\u00ean, l\u1ea1m d\u1ee5ng qu\u00e1 nhi\u1ec1u trait c\u00f3 th\u1ec3 l\u00e0m code r\u1ed1i v\u00e0 kh\u00f3 theo d\u00f5i. H\u00e3y thi\u1ebft k\u1ebf trait g\u1ecdn, r\u00f5 r\u00e0ng, v\u00e0 \u00e1p d\u1ee5ng cho nh\u1eefng h\u00e0m th\u1ef1c s\u1ef1 chung nh\u1ea5t. C\u1ea3m \u01a1n b\u1ea1n \u0111\u00e3 theo d\u00f5i series v\u1ec1 Trait trong PHP. Ch\u00fac c\u00e1c b\u1ea1n \u00e1p d\u1ee5ng hi\u1ec7u qu\u1ea3 v\u00e0 happy coding! &nbsp; T\u00e0i li\u1ec7u li\u00ean quan: PHP Manual \u2013 Traits: https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php\u00a0 PHP Manual \u2013 Conflict Resolution: https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php#traits.conflict.resolution\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dangnh.cf\/vi\/2025\/05\/19\/trait-mixin-in-php-part-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Mark&#039;s Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/dangtute\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T04:41:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-19T05:02:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"540\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 ph\u00fat\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/dangnh.cf\\\/#\\\/schema\\\/person\\\/8f3b9db3b250186af217fbeec88eda9c\"},\"headline\":\"Trait \u2013 Mixin in PHP? &#8211; PART 2\",\"datePublished\":\"2025-05-19T04:41:46+00:00\",\"dateModified\":\"2025-05-19T05:02:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/\"},\"wordCount\":849,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/dangnh.cf\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1\",\"keywords\":[\"php\",\"trait\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"vi\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/\",\"url\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/\",\"name\":\"Trait \u2013 Mixin in PHP? - PART 2 - Mark's Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/dangnh.cf\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/dangnh.cf\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1\",\"datePublished\":\"2025-05-19T04:41:46+00:00\",\"dateModified\":\"2025-05-19T05:02:04+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/dangnh.cf\\\/#\\\/schema\\\/person\\\/8f3b9db3b250186af217fbeec88eda9c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/#breadcrumb\"},\"inLanguage\":\"vi\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"vi\",\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/dangnh.cf\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/dangnh.cf\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1\",\"width\":960,\"height\":540,\"caption\":\"Trait in PHP\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/dangnh.cf\\\/2025\\\/05\\\/19\\\/trait-mixin-in-php-part-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/dangnh.cf\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Trait \u2013 Mixin in PHP? &#8211; PART 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/dangnh.cf\\\/#website\",\"url\":\"https:\\\/\\\/dangnh.cf\\\/\",\"name\":\"Mark's Blog\",\"description\":\"A blog about Programming and Technical Stuffs\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/dangnh.cf\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"vi\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/dangnh.cf\\\/#\\\/schema\\\/person\\\/8f3b9db3b250186af217fbeec88eda9c\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"vi\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d838feab6bbd5e774c608b656a29ab0d54981b88fa4b563a83d7635108b6c76e?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d838feab6bbd5e774c608b656a29ab0d54981b88fa4b563a83d7635108b6c76e?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d838feab6bbd5e774c608b656a29ab0d54981b88fa4b563a83d7635108b6c76e?s=96&d=retro&r=g\",\"caption\":\"admin\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Trait \u2013 Mixin in PHP? - PART 2 - Mark's Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/dangnh.cf\/vi\/2025\/05\/19\/trait-mixin-in-php-part-2\/","og_locale":"vi_VN","og_type":"article","og_title":"Trait \u2013 Mixin in PHP? - PART 2 - Mark's Blog","og_description":"D\u01b0\u1edbi \u0111\u00e2y l\u00e0 ph\u1ea7n 2 (v\u00e0 c\u0169ng l\u00e0 ph\u1ea7n cu\u1ed1i) c\u1ee7a series \u201cTrait \u2013 Mixin trong PHP\u201d. \u1ede ph\u1ea7n n\u00e0y ch\u00fang ta s\u1ebd \u0111i s\u00e2u v\u00e0o c\u00e1ch x\u1eed l\u00fd xung \u0111\u1ed9t, m\u1edf r\u1ed9ng kh\u1ea3 n\u0103ng c\u1ee7a Trait v\u00e0 c\u00e1c v\u00ed d\u1ee5 th\u1ef1c ti\u1ec5n. M\u1eddi c\u00e1c b\u1ea1n xem l\u1ea1i ph\u1ea7n 1 t\u1ea1i \u0111\u00e2y: https:\/\/dangnh.cf\/2018\/06\/01\/trait-mixin-in-php\/ 1. Gi\u1ea3i quy\u1ebft xung \u0111\u1ed9t gi\u1eefa c\u00e1c Trait Khi m\u1ed9t class use nhi\u1ec1u trait v\u00e0 c\u00f3 ph\u01b0\u01a1ng th\u1ee9c tr\u00f9ng t\u00ean, PHP s\u1ebd b\u00e1o l\u1ed7i. \u0110\u1ec3 tr\u00e1nh, ta d\u00f9ng hai t\u1eeb kh\u00f3a: 1.1. insteadof Ch\u1ecdn ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a trait n\u00e0y thay v\u00ec c\u1ee7a trait kia: trait A { public function foo() { echo &quot;A::foon&quot;; } } trait B { public function foo() { echo &quot;B::foon&quot;; } } class MyClass { use A, B { A::foo insteadof B; \/\/ \u01b0u ti\u00ean A::foo } } (new MyClass)-&amp;gt;foo(); \/\/ In ra &quot;A::foo&quot; 1.2. as \u0110\u1ed5i t\u00ean (alias) cho ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ec3 v\u1eabn c\u00f3 th\u1ec3 g\u1ecdi c\u1ea3 hai: class MyClass { use A, B { A::foo insteadof B; B::foo as bar; \/\/ g\u1ecdi B::foo qua t\u00ean m\u1edbi bar() } } $obj = new MyClass; $obj-&amp;gt;foo(); \/\/ A::foo $obj-&amp;gt;bar(); \/\/ B::foo 2. C\u00e1c t\u00ednh n\u0103ng n\u00e2ng cao c\u1ee7a Trait 2.1. Abstract method trong Trait B\u1ea1n c\u00f3 th\u1ec3 khai b\u00e1o ph\u01b0\u01a1ng th\u1ee9c abstract trong trait, bu\u1ed9c class k\u1ebf th\u1eeba ph\u1ea3i tri\u1ec3n khai: trait LoggerTrait { abstract protected function getLogFile(): string; public function log($msg) { file_put_contents($this-&amp;gt;getLogFile(), $msg.&quot;n&quot;, FILE_APPEND); } } class App { use LoggerTrait; protected function getLogFile(): string { return __DIR__ . '\/app.log'; } } 2.2. Trait v\u1edbi thu\u1ed9c t\u00ednh static v\u00e0\u00a0const Trait cho ph\u00e9p \u0111\u1ecbnh ngh\u0129a static property v\u00e0 const: trait Configurable { protected static array $options = &#x5B;]; public static function setOption($k, $v) { static::$options&#x5B;$k] = $v; } public static function getOption($k) { return static::$options&#x5B;$k] ?? null; } } 2.3. Thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c private\/protected Trait c\u00f3 th\u1ec3 khai b\u00e1o method private hay protected, ch\u1ec9 class ch\u1ee9a trait bi\u1ebft d\u00f9ng: trait SecretTrait { private function secret() { return &quot;Ch\u1ec9 c\u00f3 t\u00f4i m\u1edbi bi\u1ebft&quot;; } public function reveal() { return $this-&amp;gt;secret(); } } 2.4. Trait th\u1eeba k\u1ebf Trait Trait c\u0169ng c\u00f3 th\u1ec3 use trait kh\u00e1c: trait A { \/* ... *\/ } trait B { use A; \/* ... *\/ } class C { use B; \/\/ s\u1ebd bao g\u1ed3m c\u1ea3 A } 3. V\u00ed d\u1ee5 th\u1ef1c t\u1ebf 3.1. Trait \u201cSoftDelete\u201d cho Model trait SoftDelete { protected bool $deleted = false; public function delete() { $this-&amp;gt;deleted = true; } public function restore() { $this-&amp;gt;deleted = false; } public function isDeleted(): bool { return $this-&amp;gt;deleted; } } class User { use SoftDelete; \/\/ ... } Gi\u1edd ch\u1ec9 c\u1ea7n use SoftDelete, m\u1ecdi class \u0111\u1ec1u c\u00f3 t\u00ednh n\u0103ng x\u00f3a m\u1ec1m. 3.2. Trait \u201cTimestampable\u201d T\u1ef1 \u0111\u1ed9ng qu\u1ea3n l\u00fd created_at\/updated_at: trait Timestampable { protected DateTime $createdAt; protected DateTime $updatedAt; public function touch() { $this-&amp;gt;updatedAt = new DateTime; } public function initTimestamps() { $this-&amp;gt;createdAt = $this-&amp;gt;updatedAt = new DateTime; } } class Post { use Timestampable; public function __construct() { $this-&amp;gt;initTimestamps(); } } 4. Best Practices khi d\u00f9ng Trait Gi\u1eef trait nh\u1ecf, chuy\u00ean bi\u1ec7t: M\u1ed7i trait ch\u1ec9 \u0111\u1ea3m nh\u1eadn m\u1ed9t nhi\u1ec7m v\u1ee5 duy nh\u1ea5t. \u0110\u1eb7t t\u00ean r\u00f5 r\u00e0ng: V\u00ed d\u1ee5 Loggable, Notifiable, SoftDeleteable\u2026 Tr\u00e1nh trait qu\u00e1 \u201cn\u1eb7ng\u201d: N\u1ebfu trait ch\u1ee9a qu\u00e1 nhi\u1ec1u logic, c\u00e2n nh\u1eafc t\u00e1ch th\u00e0nh class helper ho\u1eb7c service. T\u00e0i li\u1ec7u \u0111\u1ea7y \u0111\u1ee7: Khai b\u00e1o @method hay @property trong DocBlock n\u1ebfu trait \u201cinject\u201d th\u00eam method hay property cho IDE. 5. K\u1ebft lu\u1eadn Trait l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd gi\u00fap t\u00e1i s\u1eed d\u1ee5ng code v\u00e0 \u201cmixin\u201d behavior v\u00e0o nhi\u1ec1u class kh\u00e1c nhau m\u00e0 kh\u00f4ng c\u1ea7n \u0111a k\u1ebf th\u1eeba. Tuy nhi\u00ean, l\u1ea1m d\u1ee5ng qu\u00e1 nhi\u1ec1u trait c\u00f3 th\u1ec3 l\u00e0m code r\u1ed1i v\u00e0 kh\u00f3 theo d\u00f5i. H\u00e3y thi\u1ebft k\u1ebf trait g\u1ecdn, r\u00f5 r\u00e0ng, v\u00e0 \u00e1p d\u1ee5ng cho nh\u1eefng h\u00e0m th\u1ef1c s\u1ef1 chung nh\u1ea5t. C\u1ea3m \u01a1n b\u1ea1n \u0111\u00e3 theo d\u00f5i series v\u1ec1 Trait trong PHP. Ch\u00fac c\u00e1c b\u1ea1n \u00e1p d\u1ee5ng hi\u1ec7u qu\u1ea3 v\u00e0 happy coding! &nbsp; T\u00e0i li\u1ec7u li\u00ean quan: PHP Manual \u2013 Traits: https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php\u00a0 PHP Manual \u2013 Conflict Resolution: https:\/\/www.php.net\/manual\/en\/language.oop5.traits.php#traits.conflict.resolution","og_url":"https:\/\/dangnh.cf\/vi\/2025\/05\/19\/trait-mixin-in-php-part-2\/","og_site_name":"Mark's Blog","article_publisher":"https:\/\/www.facebook.com\/dangtute","article_published_time":"2025-05-19T04:41:46+00:00","article_modified_time":"2025-05-19T05:02:04+00:00","og_image":[{"width":960,"height":540,"url":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"admin","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"4 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/#article","isPartOf":{"@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/"},"author":{"name":"admin","@id":"https:\/\/dangnh.cf\/#\/schema\/person\/8f3b9db3b250186af217fbeec88eda9c"},"headline":"Trait \u2013 Mixin in PHP? &#8211; PART 2","datePublished":"2025-05-19T04:41:46+00:00","dateModified":"2025-05-19T05:02:04+00:00","mainEntityOfPage":{"@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/"},"wordCount":849,"commentCount":0,"image":{"@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1","keywords":["php","trait"],"articleSection":["PHP"],"inLanguage":"vi","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/","url":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/","name":"Trait \u2013 Mixin in PHP? - PART 2 - Mark's Blog","isPartOf":{"@id":"https:\/\/dangnh.cf\/#website"},"primaryImageOfPage":{"@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/#primaryimage"},"image":{"@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1","datePublished":"2025-05-19T04:41:46+00:00","dateModified":"2025-05-19T05:02:04+00:00","author":{"@id":"https:\/\/dangnh.cf\/#\/schema\/person\/8f3b9db3b250186af217fbeec88eda9c"},"breadcrumb":{"@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/#primaryimage","url":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1","contentUrl":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1","width":960,"height":540,"caption":"Trait in PHP"},{"@type":"BreadcrumbList","@id":"https:\/\/dangnh.cf\/2025\/05\/19\/trait-mixin-in-php-part-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dangnh.cf\/"},{"@type":"ListItem","position":2,"name":"Trait \u2013 Mixin in PHP? &#8211; PART 2"}]},{"@type":"WebSite","@id":"https:\/\/dangnh.cf\/#website","url":"https:\/\/dangnh.cf\/","name":"Mark's Blog","description":"A blog about Programming and Technical Stuffs","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dangnh.cf\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Person","@id":"https:\/\/dangnh.cf\/#\/schema\/person\/8f3b9db3b250186af217fbeec88eda9c","name":"admin","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/secure.gravatar.com\/avatar\/d838feab6bbd5e774c608b656a29ab0d54981b88fa4b563a83d7635108b6c76e?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d838feab6bbd5e774c608b656a29ab0d54981b88fa4b563a83d7635108b6c76e?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d838feab6bbd5e774c608b656a29ab0d54981b88fa4b563a83d7635108b6c76e?s=96&d=retro&r=g","caption":"admin"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9w3NP-6y","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":211,"url":"https:\/\/dangnh.cf\/vi\/2018\/06\/01\/trait-mixin-in-php\/","url_meta":{"origin":406,"position":0},"title":"Trait &#8211; Mixin in PHP?","author":"admin","date":"Th\u00e1ng 6 1, 2018","format":false,"excerpt":"Hi guys :) Nh\u01b0 c\u00e1c b\u1ea1n c\u00f3 l\u1ebd \u0111\u00e3 bi\u1ebft, PHP l\u00e0 1 ng\u00f4n ng\u1eef single inheritance, t\u1ee9c l\u00e0 n\u00f3 s\u1ebd c\u00f3 1 s\u1ed1 nh\u01b0\u1ee3c \u0111i\u1ec3m nh\u01b0: M\u1ea5t th\u1eddi gian & c\u00f4ng s\u1ee9c khi t\u00ecm hi\u1ec3u\/ch\u1ea1y h\u1ec7 th\u1ed1ng. V\u00ed d\u1ee5: 1 class c\u00f3 10 levels c\u00e1c class cha, th\u00ec v\u1ec1\u2026","rel":"","context":"Trong &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/dangnh.cf\/vi\/category\/web-development\/php\/"},"img":{"alt_text":"Trait in PHP","src":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":409,"url":"https:\/\/dangnh.cf\/vi\/2025\/05\/19\/huong-dan-setup-ai-blog-to-podcast-agent\/","url_meta":{"origin":406,"position":1},"title":"H\u01b0\u1edbng D\u1eabn Setup AI Blog to Podcast Agent","author":"admin","date":"Th\u00e1ng 5 19, 2025","format":false,"excerpt":"D\u01b0\u1edbi \u0111\u00e2y l\u00e0 b\u00e0i vi\u1ebft gi\u1edbi thi\u1ec7u nhanh v\u1ec1 repo Awesome LLM Apps v\u00e0 h\u01b0\u1edbng d\u1eabn ch\u1ea1y th\u1eed \u201cAI Blog to Podcast Agent\u201d tr\u00ean m\u00e1y Mac c\u1ee7a b\u1ea1n. M\u00ecnh s\u1ebd l\u1ea7n l\u01b0\u1ee3t \u0111i qua: Gi\u1edbi thi\u1ec7u chung v\u1ec1 repo T\u1ea1i sao n\u00ean kh\u00e1m ph\u00e1 \u0110i\u1ec3m nh\u1ea5n \u201cAI Blog to Podcast\u2026","rel":"","context":"Trong &quot;AI&quot;","block_context":{"text":"AI","link":"https:\/\/dangnh.cf\/vi\/category\/ai\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2025\/05\/ai-agent.png?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2025\/05\/ai-agent.png?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2025\/05\/ai-agent.png?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2025\/05\/ai-agent.png?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":124,"url":"https:\/\/dangnh.cf\/vi\/2017\/05\/25\/stdclass-la-gi-lam-the-nao-de-co-properties-dong-trong-php\/","url_meta":{"origin":406,"position":2},"title":"stdClass l\u00e0 g\u00ec? L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 c\u00f3 Dynamic Properties trong PHP?","author":"admin","date":"Th\u00e1ng 5 25, 2017","format":false,"excerpt":"Ch\u00e0o c\u00e1c b\u1ea1n, m\u00ecnh l\u00e0 \u0110\u0103ng, v\u00e0 m\u00ecnh \u0111\u00e3 tr\u1edf l\u1ea1i r\u1ed3i \u0111\u00e2yy\u00a0 N\u1ebfu b\u1ea1n l\u00e0 1 ng\u01b0\u1eddi r\u1ea5t hay t\u00f2 m\u00f2 ngh\u1ecbch ng\u1ee3m, gi\u1ed1ng t\u00f4i, th\u00ec ch\u1eafc h\u1eb3n b\u1ea1n \u0111\u00e3 t\u1eebng lang thang trong core c\u1ee7a m\u1ea5y th\u1eb1ng PHP framework nh\u01b0 Laravel hay Yii, v\u00e0 ch\u1eafc cmn c\u00fa l\u00e0\u2026","rel":"","context":"Trong &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/dangnh.cf\/vi\/category\/web-development\/php\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2017\/05\/stdclass-trong-php.png?fit=365%2C268&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":367,"url":"https:\/\/dangnh.cf\/vi\/2020\/01\/20\/service-container-di-sanh-dieu-voi-laravel\/","url_meta":{"origin":406,"position":3},"title":"Service Container &#038; DI &#8220;s\u00e0nh \u0111i\u1ec7u&#8221; v\u1edbi Laravel","author":"admin","date":"Th\u00e1ng 1 20, 2020","format":false,"excerpt":"Hi, long time no see :D Laravel l\u00e0 m\u1ed9t framework n\u1ed5i ti\u1ebfng nh\u1ea5t trong c\u1ed9ng \u0111\u1ed3ng PHP, 1 ph\u1ea7n l\u00e0 v\u00ec c\u1ea5u tr\u00fac tuy\u1ec7t v\u1eddi c\u1ee7a n\u00f3. Khi n\u00f3i \u0111\u1ebfn architectural concepts c\u1ee7a Laravel, kh\u00f4ng th\u1ec3 kh\u00f4ng nh\u1eafc t\u1edbi Service Container. Khi b\u1ea1n \u0111\u1ee5ng \u0111\u1ebfn 1 b\u00e0i to\u00e1n l\u1edbn h\u01a1n,\u2026","rel":"","context":"Trong &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/dangnh.cf\/vi\/category\/web-development\/php\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2020\/01\/1_SstfI6yiEWj1mrnFrLTUdA-e1579505803935.jpeg?fit=900%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2020\/01\/1_SstfI6yiEWj1mrnFrLTUdA-e1579505803935.jpeg?fit=900%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2020\/01\/1_SstfI6yiEWj1mrnFrLTUdA-e1579505803935.jpeg?fit=900%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2020\/01\/1_SstfI6yiEWj1mrnFrLTUdA-e1579505803935.jpeg?fit=900%2C600&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":66,"url":"https:\/\/dangnh.cf\/vi\/2017\/05\/19\/lam-toan-giai-tri-mot-ti-nhi\/","url_meta":{"origin":406,"position":4},"title":"L\u00e0m to\u00e1n gi\u1ea3i tr\u00ed m\u1ed9t t\u00ed nh\u1ec9?","author":"admin","date":"Th\u00e1ng 5 19, 2017","format":false,"excerpt":"Ok, ch\u00e0o t\u1ea5t c\u1ea3 anh em Ch\u1ea3 l\u00e0 cu\u1ed1i tu\u1ea7n, \u1edf nh\u00e0 r\u1ea3nh rang, tr\u1eddi th\u00ec se se l\u1ea1nh, m\u0169i h\u01a1i t\u1eafc, ch\u1ec9 mu\u1ed1n co ro qu\u1ea5n ch\u0103n \u00f4m laptop cho \u1ea5m. Lang thang thi c\u00e1i cu\u1ed9c thi l\u1eadp tr\u00ecnh c\u1ee7a b\u1ecdn Top Career g\u00ec \u0111\u00f3 :v v\u00f4 t\u00ecnh v\u1ea5p\u2026","rel":"","context":"Trong &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/dangnh.cf\/vi\/category\/web-development\/php\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2017\/05\/learning_math-e1459355151809.jpg?fit=800%2C513&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2017\/05\/learning_math-e1459355151809.jpg?fit=800%2C513&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2017\/05\/learning_math-e1459355151809.jpg?fit=800%2C513&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2017\/05\/learning_math-e1459355151809.jpg?fit=800%2C513&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":112,"url":"https:\/\/dangnh.cf\/vi\/2017\/05\/25\/what-is-stdclass-and-dynamic-properties-in-php\/","url_meta":{"origin":406,"position":5},"title":"What is stdClass? And Dynamic Properties in PHP?","author":"admin","date":"Th\u00e1ng 5 25, 2017","format":false,"excerpt":"Well hello there, I\u2019m back If you are a curious person, like me, you must have wandered in the core code of PHP frameworks like Laravel or Yii, and you must have seen this\u00a0stdClass here and there. Now, let\u2019s see what is that and how it helps us to code\u2026","rel":"","context":"Trong &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/dangnh.cf\/vi\/category\/web-development\/php\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2017\/05\/stdclass-trong-php.png?fit=365%2C268&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/posts\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":5,"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/posts\/406\/revisions\/413"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/media\/349"}],"wp:attachment":[{"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dangnh.cf\/vi\/wp-json\/wp\/v2\/tags?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}