<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel><title>examples | Hugo Ferreira</title>
    <link>https://hugo.ferreira.cc/tags/examples/</link>
    <description>Recent content in Examples by Hugo Ferreira</description>
    <image>
      <title>examples | Hugo Ferreira</title>
      <url>https://hugo.ferreira.cc/hf-bw.jpg</url>
      <link>https://hugo.ferreira.cc/tags/examples/</link>
    </image>
    <generator>Hugo -- 0.124.1</generator>
    <language>en</language>
    <copyright>2000–2024 by Hugo Ferreira · CC BY 4.0</copyright>
    <lastBuildDate>Mon, 15 Apr 2019 00:46:48 +0000</lastBuildDate>
    <atom:link href="https://hugo.ferreira.cc/tags/examples/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>💭 PHP shenanigans</title>
      <link>https://hugo.ferreira.cc/php-shenanigans/</link>
      <pubDate>Mon, 15 Apr 2019 00:46:48 +0000</pubDate>
      <guid>https://hugo.ferreira.cc/php-shenanigans/</guid>
      <description>&lt;p&gt;Pop quiz about &lt;a href=&#34;https://www.php.net/&#34;&gt;PHP&lt;/a&gt; and something we’ve stumbled upon last week, while working on a client’s codebase.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Pop quiz about <a href="https://www.php.net/">PHP</a> and something we’ve stumbled upon last week, while working on a client’s codebase.</p>
<p>Given the following code:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-php" data-lang="php"><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">Foo</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="k">public</span> <span class="nv">$bar</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="k">public</span> <span class="k">function</span> <span class="fm">__construct</span><span class="p">(</span><span class="nv">$value</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">bar</span> <span class="o">=</span> <span class="nv">$value</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nv">$a</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Foo</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="nv">$b</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Foo</span><span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="nv">$c</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Foo</span><span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nv">$result</span> <span class="o">=</span> <span class="nx">array_diff</span><span class="p">(</span> <span class="p">[</span><span class="nv">$a</span><span class="p">,</span> <span class="nv">$b</span><span class="p">,</span> <span class="nv">$c</span><span class="p">],</span> <span class="p">[</span><span class="nv">$a</span><span class="p">,</span> <span class="nv">$b</span><span class="p">]</span> <span class="p">);</span>
</span></span></code></pre></div><p>What would you <em>expect</em> the result to be?</p>
<p>1️⃣ <code>Array( )</code><br>
2️⃣ <code>Array( [0] =&gt; Foo( [bar] =&gt; c ) )</code><br>
3️⃣ <code>Array( [2] =&gt; Foo( [bar] =&gt; c ) )</code><br>
4️⃣ <code>Object of class Foo could not be converted to string</code></p>
<p>I’ll give you some moments to think it over…</p>
<hr>

<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
  <iframe src="https://www.youtube.com/embed/S3wsCRJVUyg?rel=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" allowfullscreen title="More moments later…"></iframe>
</div>

<hr>

<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
  <iframe src="https://www.youtube.com/embed/u3UtCnXG2io?rel=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" allowfullscreen title="More moments later…"></iframe>
</div>

<hr>
<p>“Obviously” [not 🙄] the right answer is option <strong>4</strong> …</p>
<blockquote>
<p>4️⃣ <code>Object of class Foo could not be converted to string</code></p>
</blockquote>
<p>… because for the <a href="https://www.php.net/manual/en/function.array-diff.php"><code>array_diff</code></a> function …</p>
<blockquote>
<p>Two elements are considered equal if and only if <code>(string) $elem1 === (string) $elem2</code>. That is, when the <a href="https://www.php.net/manual/en/language.types.string.php#language.types.string.casting">string representation</a> is the same.</p>
</blockquote>
<p>… and, adding insult to injury, objects <em><strong>don’t</strong> have a default</em> string representation.</p>
<p>😱 🤦‍♂️ x 100</p>
<iframe src="https://giphy.com/embed/wj8VEUZVVEuls6pOw7" width="480" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>
<hr>
<p>Funnily enough [not at all 😤] implementing the string conversion magic method …</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-php" data-lang="php"><span class="line"><span class="cl"><span class="k">public</span> <span class="k">function</span> <span class="fm">__toString</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">bar</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>… then option <strong>3</strong> becomes the answer …</p>
<blockquote>
<p>3️⃣ <code>Array( [2] =&gt; Foo( [bar] =&gt; c ) )</code></p>
</blockquote>
<p>… because “arrays” in PHP don’t exist and, in reality, are nothig more than key-value maps in disguise, with the index numbers as the keys. 😕</p>
<p>This means you&rsquo;d need to wrap it all around an <a href="https://www.php.net/manual/en/function.array-values.php"><code>array_values</code></a> call to “reset” all the <del>indexes</del> keys of the <del>array</del> map back to zero.</p>
<iframe src="https://giphy.com/embed/zBZk5FD18QhjP35Goa" width="480" height="480" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>
<hr>
<p>Guess I need to make this old article my new bed time reading, to read over and over and over again: <a href="https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/">PHP: a fractal of bad design / fuzzy notepad</a></p>]]></content:encoded>
    </item>
    <item>
      <title>🔗 Mobile cross-platform technologies comparison</title>
      <link>https://hugo.ferreira.cc/mobile-cross-platform-comparison/</link>
      <pubDate>Thu, 29 Jan 2015 00:00:00 +0000</pubDate>
      <guid>https://hugo.ferreira.cc/mobile-cross-platform-comparison/</guid>
      <description>{width=&amp;ldquo;496&amp;rdquo; height=&amp;ldquo;1290&amp;rdquo; srcset=&amp;ldquo;tumblr_niy2fjyTFG1qz82meo2_r1_1280.png 496w, tumblr_niy2fjyTFG1qz82meo2_r1_1280-115x300.png 115w, tumblr_niy2fjyTFG1qz82meo2_r1_1280-394x1024.png 394w&amp;rdquo; sizes=&amp;quot;(max-width: 496px) 100vw, 496px&amp;quot;}
Developers are now finding themselves having to author applications for a diverse range of mobile platforms (iOS, Android, Windows Phone, &amp;hellip;), each of which have their own &amp;rsquo;native&amp;rsquo; development languages, tools and environment. (&amp;hellip;) but which to choose?
(&amp;hellip;)
PropertyCross presents a non-trivial application , for searching UK property listings, developed using a range of cross-platform technologies and frameworks.</description>
      <content:encoded><![CDATA[<p><a href="http://propertycross.com/"><img loading="lazy" src="tumblr_niy2fjyTFG1qz82meo2_r1_1280.png" alt="PropertyCross"  />
{width=&ldquo;496&rdquo;
height=&ldquo;1290&rdquo;
srcset=&ldquo;tumblr_niy2fjyTFG1qz82meo2_r1_1280.png 496w, tumblr_niy2fjyTFG1qz82meo2_r1_1280-115x300.png 115w, tumblr_niy2fjyTFG1qz82meo2_r1_1280-394x1024.png 394w&rdquo;
sizes=&quot;(max-width: 496px) 100vw, 496px&quot;}</a></p>
<blockquote>
<p>Developers are now finding themselves having to author applications
for a diverse range of mobile platforms (iOS, Android, Windows Phone,
&hellip;), each of which have their own &rsquo;native&rsquo; development languages,
tools and environment. (&hellip;) but which to choose?</p>
<p>(&hellip;)</p>
<p><strong><a href="http://propertycross.com/">PropertyCross</a></strong> presents a
<strong>non-trivial application</strong> , for searching UK property listings,
developed <strong>using a range of cross-platform technologies</strong> and
frameworks. Our aim is to provide developers with a practical insight
into the strengths and weaknesses of each framework.</p>
</blockquote>
]]></content:encoded>
    </item>
    <item>
      <title>🔗 Custom UI Controls for iOS and Mac OS X -- Cocoa Controls</title>
      <link>https://hugo.ferreira.cc/custom-ui-controls-for-ios-and-mac-os-x-cocoa/</link>
      <pubDate>Tue, 14 Aug 2012 07:07:28 +0000</pubDate>
      <guid>https://hugo.ferreira.cc/custom-ui-controls-for-ios-and-mac-os-x-cocoa/</guid>
      <description>Custom UI Controls for iOS and Mac OS X &amp;ndash; Cocoa Controls
Making an app? Don&amp;rsquo;t re-invent the wheel. Save time by using a control someone&amp;rsquo;s already written.</description>
      <content:encoded><![CDATA[<p><a href="http://www.cocoacontrols.com/">Custom UI Controls for iOS and Mac OS X &ndash; Cocoa
Controls</a></p>
<blockquote>
<p>Making an app? Don&rsquo;t re-invent the wheel. Save time by using a control
someone&rsquo;s already written.</p>
</blockquote>
]]></content:encoded>
    </item>
    <item>
      <title>🔗 iPhone SDK Examples</title>
      <link>https://hugo.ferreira.cc/iphone-sdk-examples/</link>
      <pubDate>Tue, 10 Jul 2012 07:07:28 +0000</pubDate>
      <guid>https://hugo.ferreira.cc/iphone-sdk-examples/</guid>
      <description>iPhone SDK Examples
&amp;ldquo;Just show me how to do it!&amp;rdquo;
This site is meant for the relatively new iPhone Developer who just wants simple examples to common tasks.
It is meant as a starting point. Please take the time to read the documentation and understand the SDK in depth.</description>
      <content:encoded><![CDATA[<p><a href="http://www.iphoneexamples.com/">iPhone SDK Examples</a></p>
<blockquote>
<p>&ldquo;Just show me how to do it!&rdquo;</p>
<p>This site is meant for the relatively new iPhone Developer who just
wants simple examples to common tasks.</p>
<p>It is meant as a starting point. Please take the time to read the
documentation and understand the SDK in depth.</p>
</blockquote>
]]></content:encoded>
    </item>
  </channel>
</rss>
