{"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":"What is stdClass? And Dynamic Properties in PHP? - Mark's Blog","type":"rich","width":600,"height":338,"html":"<blockquote class=\"wp-embedded-content\" data-secret=\"YaAioXtcRE\"><a href=\"https:\/\/dangnh.cf\/vi\/2017\/05\/25\/what-is-stdclass-and-dynamic-properties-in-php\/\">What is stdClass? And Dynamic Properties in PHP?<\/a><\/blockquote><iframe sandbox=\"allow-scripts\" security=\"restricted\" src=\"https:\/\/dangnh.cf\/vi\/2017\/05\/25\/what-is-stdclass-and-dynamic-properties-in-php\/embed\/#?secret=YaAioXtcRE\" width=\"600\" height=\"338\" title=\"&#8220;What is stdClass? And Dynamic Properties in PHP?&#8221; &#8212; Mark&#039;s Blog\" data-secret=\"YaAioXtcRE\" 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\/2017\/05\/stdclass-trong-php.png","thumbnail_width":365,"thumbnail_height":268,"description":"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 cleaner and more efficient. Okay if you came from or know even just a little Java, you\u2019ll familiar with this concept called\u00a0Dynamic Properties, to create a new object in Java somehow it\u2019ll look like this: Well, before PHP 5.4 object\u2019s properties must be predefined before we can set or get it with the magic method. What a bumper! But luckily, now we can define a new object as quick and dynamic as this: See?\u00a0Sometimes all that is necessary is a property bag to throw key value pairs into. One way is to use array, but this requires quoting all keys.\u00a0Another way is to use dynamic properties on an instance of StdClass. StdClass is a sparsely documented class in PHP which has no predefined properties. You can even cast an array directly to an object in a sec: To conclusion,\u00a0Objects are really useful when you\u2019re working with a large data structure, as you can have an object with nested sub-arrays in it. And\u00a0StdClass\u00a0(std stand for standard)is just a generic \u2019empty bag\u2019 class that\u2019s used when casting other types to objects with what ever properties you want in them. BUT\u00a0despite what the others say, StdClass\u00a0is not the base class for objects in PHP. Please remember that. Next week I\u2019ll be back with you guys to discuss about\u00a0Trait\u00a0 See ya Dang.NH"}