{"id":26949,"date":"2016-11-24T15:15:33","date_gmt":"2016-11-24T13:15:33","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=26949"},"modified":"2016-11-24T15:15:33","modified_gmt":"2016-11-24T13:15:33","slug":"quick-way-to-create-a-php-stdclass","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2016\/11\/24\/quick-way-to-create-a-php-stdclass\/","title":{"rendered":"Quick way to create a PHP stdClass"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p><a href=\"https:\/\/www.simonholywell.com\/post\/2016\/11\/quick-way-to-create-php-stdclass\/\">Simon Holywell<\/a> shows how to quickly create the stdClass in PHP and populate it with properties and values, by casting an array to an object:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$x = (object) &#x5B;\r\n    'a' =&gt; 'test',\r\n    'b' =&gt; 'test2',\r\n    'c' =&gt; 'test3'\r\n];\r\nvar_dump($x);\r\n\r\n\/*\r\nobject(stdClass)#1 (3) {\r\n  &#x5B;&quot;a&quot;]=&gt;\r\n  string(4) &quot;test&quot;\r\n  &#x5B;&quot;b&quot;]=&gt;\r\n  string(5) &quot;test2&quot;\r\n  &#x5B;&quot;c&quot;]=&gt;\r\n  string(5) &quot;test3&quot;\r\n}\r\n*\/\r\n<\/pre>\n<p>A couple of things to keep in mind here are:<\/p>\n<ol>\n<li>In PHP, an associative array key have multiple same keys. \u00a0If you cast such an associative array to object, the latest key will silently overwrite the value of the previous ones.<\/li>\n<li>The order of properties in the object will not necessarily match the order of keys in the associative array.<\/li>\n<\/ol>\n<p>Very handy!<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>Simon Holywell shows how to quickly create the stdClass in PHP and populate it with properties and values, by casting an array to an object: $x = (object) &#x5B; &#8216;a&#8217; =&gt; &#8216;test&#8217;, &#8216;b&#8217; =&gt; &#8216;test2&#8217;, &#8216;c&#8217; =&gt; &#8216;test3&#8217; ]; var_dump($x); \/* object(stdClass)#1 (3) { &#x5B;&quot;a&quot;]=&gt; string(4) &quot;test&quot; &#x5B;&quot;b&quot;]=&gt; string(5) &quot;test2&quot; &#x5B;&quot;c&quot;]=&gt; string(5) &quot;test3&quot; } *\/ &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2016\/11\/24\/quick-way-to-create-a-php-stdclass\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Quick way to create a PHP stdClass<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Quick way to create a PHP stdClass #PHP #WebDev #programming","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},"jetpack_post_was_ever_published":false,"_links_to":"","_links_to_target":""},"categories":[1,18,62,1334],"tags":[38,1330],"keyring_services":[],"class_list":["post-26949","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","category-web-work","tag-php","tag-web-development"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":29035,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/11\/27\/avoid-complex-arrays-in-php\/","url_meta":{"origin":26949,"position":0},"title":"Avoid complex arrays in PHP","author":"Leonid Mamchenkov","date":"November 27, 2018","format":false,"excerpt":"Now that PHP 7+ sorted out a whole bunch of problems with type-hinting of parameters, return values, variables and properties, we turn our attention to somewhat deeper issues. Array is a native citizen in PHP.\u00a0 Arrays are very convenient and are widely used.\u00a0 However, if you stop and think about\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11295,"url":"https:\/\/mamchenkov.net\/wordpress\/2008\/06\/05\/follow-up-to-where-did-all-the-php-programmers-go\/","url_meta":{"origin":26949,"position":1},"title":"Follow-up to &#8220;Where did all the PHP programmers go?&#8221;","author":"Leonid Mamchenkov","date":"June 5, 2008","format":false,"excerpt":"This is a quick follow-up to yesterday's post - \"Where did all the PHP programmers go?\". First of all, let me take the moment and say \"Wow!\".\u00a0 Somebody submitted the post to Reddit and it made it to the front page and got an unbelievable amount of comments.\u00a0 Almost 500,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27408,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/","url_meta":{"origin":26949,"position":2},"title":"Validating JSON against schema in PHP","author":"Leonid Mamchenkov","date":"March 11, 2017","format":false,"excerpt":"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). \u00a0Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=\"true\"] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12274,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/03\/20\/cakephp-graphviz-making-sense-of-a-numerous-models\/","url_meta":{"origin":26949,"position":3},"title":"CakePHP + GraphViz = making sense of a numerous models","author":"Leonid Mamchenkov","date":"March 20, 2010","format":false,"excerpt":"NOTE: THIS IS VERY MUCH OUTDATED! Read about the update or go directly to GitHub repository for the new version. \u00a0 I have a task at hand.\u00a0 I have to re-introduce myself to a rather large codebase.\u00a0 It's a project that migrated to CakePHP a couple of years ago and\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2010\/03\/graph.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":28182,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/11\/06\/object-graph-visualizing-php-objects\/","url_meta":{"origin":26949,"position":4},"title":"object-graph &#8211; visualizing PHP objects","author":"Leonid Mamchenkov","date":"November 6, 2017","format":false,"excerpt":"As you might know, I am a big fan of GraphViz.\u00a0 I've used numerous times for visualizing different parts of the project code and dependencies (see here and here for example). Today I came across a way to visualize PHP objects (not just classes) - object-graph library by Sebastian Bergmann,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/11\/object-graph-500x136.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":27320,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/02\/10\/php-assertions\/","url_meta":{"origin":26949,"position":5},"title":"PHP assertions","author":"Leonid Mamchenkov","date":"February 10, 2017","format":false,"excerpt":"When I hear the word \"assertion\", the first thing that comes to my mind is PHPUnit assertions. \u00a0Sure, I write assertions in my unit tests. \u00a0But is that the only application? \u00a0Today I decided to figure it out, or, at least, learn more about the subject. It turns out that\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=26949"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/26949\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=26949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=26949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=26949"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=26949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}