{"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 - Mixin in PHP?  - Mark's Blog","type":"rich","width":600,"height":338,"html":"<blockquote class=\"wp-embedded-content\" data-secret=\"8sgZoPtI4F\"><a href=\"https:\/\/dangnh.cf\/vi\/2018\/06\/01\/trait-mixin-in-php\/\">Trait &#8211; Mixin in PHP? <p1><\/a><\/blockquote><iframe sandbox=\"allow-scripts\" security=\"restricted\" src=\"https:\/\/dangnh.cf\/vi\/2018\/06\/01\/trait-mixin-in-php\/embed\/#?secret=8sgZoPtI4F\" width=\"600\" height=\"338\" title=\"&#8220;Trait &#8211; Mixin in PHP? &lt;p1&gt;&#8221; &#8212; Mark&#039;s Blog\" data-secret=\"8sgZoPtI4F\" 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:\/\/dangnh.cf\/wp-content\/uploads\/2018\/06\/retro-background-3101430_960_720.jpg","thumbnail_width":960,"thumbnail_height":540,"description":"Hi guys \ud83d\ude42 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 &amp; 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 c\u01a1 b\u1ea3n n\u00f3 s\u1ebd ph\u1ea3i ch\u1ea1y qua c\u1ea3 10 levels \u0111\u00f3 \u0111\u1ec3 ch\u1ea1y 1 function m\u00e0 n\u00f3 th\u1eeba k\u1ebf Class cha v\u00e0 class con b\u1ecb r\u00e0ng bu\u1ed9c v\u1edbi nhau. T\u1ee9c l\u00e0 ta kh\u00f4ng th\u1ec3 t\u00e1ch ri\u00eang bi\u1ec7t 1 class n\u00e0o \u0111\u1ec3 s\u1eed d\u1ee5ng \u0111\u01b0\u1ee3c. Theo th\u1eddi gian, th\u00eam, s\u1eeda, x\u00f3a c\u00e1c ch\u1ee9c n\u0103ng c\u1ea7n ph\u1ea3i r\u1ea5t c\u1ea9n th\u1eadn, nhi\u1ec1u tr\u01b0\u1eddng h\u1ee3p l\u00e0 g\u1ea7n nh\u01b0 kh\u00f4ng th\u1ec3 do \u0111\u1ed9 ph\u1ee9c t\u1ea1p c\u1ee7a c\u00e2y th\u1eeba k\u1ebf c\u00e1c class \u0111\u00f3. C\u1ef1c k\u00ec kh\u00f4ng linh ho\u1ea1t trong nhi\u1ec1u tr\u01b0\u1eddng h\u1ee3p. V\u00ed d\u1ee5 b\u1ea1n ch\u1ec9 c\u1ea7n 1 class \u0111\u01a1n gi\u1ea3n, ch\u1ee9a v\u00e0i c\u00e1i functions b\u1ea1n hay s\u1eed d\u1ee5ng trong h\u1ec7 th\u1ed1ng, class n\u00e0y c\u00f3 th\u1ec3 d\u00ednh d\u00e1ng \u0111\u1ebfn nhi\u1ec1u ph\u1ea7n kh\u00e1c nhau trong h\u1ec7 th\u1ed1ng, n\u00ean vi\u1ec7c th\u1eeba k\u1ebf n\u00f3 c\u00f3 v\u1ebb s\u1ebd t\u1ea1o n\u00ean 1 n\u00fat xo\u1eafn c\u1ef1c kh\u00f3 g\u1ee1 trong c\u00e2y th\u1eeba k\u1ebf c\u00e1c class. May m\u1eafn thay, t\u1eeb PHP5.4, ch\u00fang ta \u0111\u00e3 c\u00f3\u00a0Trait \u0111\u1ec3 gi\u1ea3i quy\u1ebft c\u00e1c v\u1ea5n \u0111\u1ec1 n\u00f3i tr\u00ean. Nh\u01b0ng li\u1ec7u Trait c\u00f3 ph\u1ea3i l\u00e0 Holy Grail cho PHP? Li\u1ec7u khi n\u00e0o n\u00ean d\u00f9ng Trait? Khi n\u00e0o th\u00ec ph\u1ea3i tr\u00e1nh? M\u1eddi c\u00e1c b\u1ea1n k\u00e9o xu\u1ed1ng d\u01b0\u1edbi \u0111\u1ec3 t\u00ecm hi\u1ec3u c\u00f9ng m\u00ecnh nh\u00e9 \ud83d\ude09 Trait l\u00e0 g\u00ec? Traits is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. Source &#8230; okay&#8230; h\u01a1i l\u1eb1ng nh\u1eb1ng ph\u1ea3i kh\u00f4ng? Ng\u1eafn g\u1ecdn th\u00ec Trait c\u0169ng gi\u1ed1ng nh\u01b0 1 abstract class v\u1eady. B\u1ea1n ch\u1ee9a c\u00e1c methods, properties trong \u0111\u00f3, nh\u01b0ng b\u1ea1n kh\u00f4ng th\u1ec3 instantiate (t\u1ea1o object) n\u00f3 \u0111\u01b0\u1ee3c. N\u00f3i d\u1ec5 hi\u1ec3u, Trait l\u00e0 1 m\u1edb code b\u1ea1n c\u00f3 th\u1ec3 d\u00f9ng b\u1ea5t c\u1ee9 ch\u1ed7 n\u00e0o b\u1ea1n mu\u1ed1n. Simple as that \ud83d\ude42 Nghe th\u00ec \u0111\u01a1n gi\u1ea3n th\u00f4i nh\u01b0ng m\u00e0 h\u01a1i b\u1ecb hay \u0111\u1ea5y. Next, l\u00e0m n\u00e0o \u0111\u1ec3 d\u00f9ng Trait? Ph\u1ea3i cho v\u00ed d\u1ee5 c\u1ee5 th\u1ec3 ng\u01b0\u1eddi ta m\u1edbi hi\u1ec3u \u0111\u01b0\u1ee3c ch\u1ee9, \u0111\u00fang kh\u00f4ng!? S\u1eed d\u1ee5ng Trait th\u1ebf n\u00e0o? \u1ede \u0111\u00e2y m\u00ecnh s\u1ebd tr\u00ecnh b\u00e0y cho c\u00e1c b\u1ea1n c\u00e1ch s\u1eed d\u1ee5ng Trait trong r\u1ea5t nhi\u1ec1u tr\u01b0\u1eddng h\u1ee3p, c\u00f3 th\u1ec3 s\u1ebd h\u01a1i d\u00e0i, ch\u1ecbu kh\u00f3 \u0111\u1ecdc nh\u00e9 \ud83d\ude09 V\u00ed d\u1ee5 \u0111\u01a1n gi\u1ea3n: V\u1ec1 quy\u1ec1n \u01b0u ti\u00ean, n\u1ebfu tr\u00f9ng t\u00ean function c\u1ee7a Trait v\u1edbi function c\u1ee7a Base class, th\u00ec chuy\u1ec7n g\u00ec s\u1ebd x\u1ea3y ra? To be continue&#8230;"}