{"version":"1.0","provider_name":"Mark's Blog","provider_url":"https:\/\/dangnh.cf\/vi","author_name":"admin","author_url":"https:\/\/dangnh.cf\/vi\/author\/admin\/","title":"Trait \u2013 Mixin in PHP? - PART 2 - Mark's Blog","type":"rich","width":600,"height":338,"html":"<blockquote class=\"wp-embedded-content\" data-secret=\"0oV97oU9Kr\"><a href=\"https:\/\/dangnh.cf\/vi\/2025\/05\/19\/trait-mixin-in-php-part-2\/\">Trait \u2013 Mixin in PHP? &#8211; PART 2<\/a><\/blockquote><iframe sandbox=\"allow-scripts\" security=\"restricted\" src=\"https:\/\/dangnh.cf\/vi\/2025\/05\/19\/trait-mixin-in-php-part-2\/embed\/#?secret=0oV97oU9Kr\" width=\"600\" height=\"338\" title=\"&#8220;Trait \u2013 Mixin in PHP? &#8211; PART 2&#8221; &#8212; Mark&#039;s Blog\" data-secret=\"0oV97oU9Kr\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" class=\"wp-embedded-content\"><\/iframe><script type=\"text\/javascript\">\n\/* <![CDATA[ *\/\n\/*! This file is auto-generated *\/\n!function(d,l){\"use strict\";l.querySelector&&d.addEventListener&&\"undefined\"!=typeof URL&&(d.wp=d.wp||{},d.wp.receiveEmbedMessage||(d.wp.receiveEmbedMessage=function(e){var t=e.data;if((t||t.secret||t.message||t.value)&&!\/[^a-zA-Z0-9]\/.test(t.secret)){for(var s,r,n,a=l.querySelectorAll('iframe[data-secret=\"'+t.secret+'\"]'),o=l.querySelectorAll('blockquote[data-secret=\"'+t.secret+'\"]'),c=new RegExp(\"^https?:$\",\"i\"),i=0;i<o.length;i++)o[i].style.display=\"none\";for(i=0;i<a.length;i++)s=a[i],e.source===s.contentWindow&&(s.removeAttribute(\"style\"),\"height\"===t.message?(1e3<(r=parseInt(t.value,10))?r=1e3:~~r<200&&(r=200),s.height=r):\"link\"===t.message&&(r=new URL(s.getAttribute(\"src\")),n=new URL(t.value),c.test(n.protocol))&&n.host===r.host&&l.activeElement===s&&(d.top.location.href=t.value))}},d.addEventListener(\"message\",d.wp.receiveEmbedMessage,!1),l.addEventListener(\"DOMContentLoaded\",function(){for(var e,t,s=l.querySelectorAll(\"iframe.wp-embedded-content\"),r=0;r<s.length;r++)(t=(e=s[r]).getAttribute(\"data-secret\"))||(t=Math.random().toString(36).substring(2,12),e.src+=\"#?secret=\"+t,e.setAttribute(\"data-secret\",t)),e.contentWindow.postMessage({message:\"ready\",secret:t},\"*\")},!1)))}(window,document);\n\/\/# sourceURL=https:\/\/dangnh.cf\/wp-includes\/js\/wp-embed.min.js\n\/* ]]> *\/\n<\/script>","thumbnail_url":"https:\/\/i0.wp.com\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg?fit=960%2C540&ssl=1","thumbnail_width":960,"thumbnail_height":540,"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: 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: 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: 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: 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: 2.4. Trait th\u1eeba k\u1ebf Trait Trait c\u0169ng c\u00f3 th\u1ec3 use trait kh\u00e1c: 3. V\u00ed d\u1ee5 th\u1ef1c t\u1ebf 3.1. Trait \u201cSoftDelete\u201d cho Model 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: 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"}