<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Steven Yung</title>
    <description>The latest articles on Forem by Steven Yung (@excessivecoding).</description>
    <link>https://forem.com/excessivecoding</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F468389%2Ff6815e84-7b35-4019-a682-f38b614a6a57.jpg</url>
      <title>Forem: Steven Yung</title>
      <link>https://forem.com/excessivecoding</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/excessivecoding"/>
    <language>en</language>
    <item>
      <title>Laravel Greatest Trick Revealed: Magic Methods</title>
      <dc:creator>Steven Yung</dc:creator>
      <pubDate>Mon, 15 Apr 2019 10:51:06 +0000</pubDate>
      <link>https://forem.com/excessivecoding/laravel-greatest-trick-revealed-magic-methods-31om</link>
      <guid>https://forem.com/excessivecoding/laravel-greatest-trick-revealed-magic-methods-31om</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5215dakax8frunx120d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv5215dakax8frunx120d.png" alt="Photo by Mervyn Chan on Unsplash" width="800" height="582"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Photo by Mervyn Chan on Unsplash&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Laravel has leverage PHP to a whole new level, providing awesome Developer Experience (DX) to craft your next project. As a result, some people qualify it as "magic".&lt;/p&gt;

&lt;p&gt;Today, I will reveal to you one of Laravel tricks, &lt;strong&gt;magic methods&lt;/strong&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  What is a magic method ?
&lt;/h1&gt;

&lt;p&gt;It's important to understand that magic methods are not exclusive to Laravel but are available in any PHP application that you have. Laravel just happens to have some of the most interesting use cases for magic methods.&lt;/p&gt;

&lt;p&gt;Magic methods are methods available in any classes that you declare in PHP which provide way to implement additional functionality in your class.&lt;/p&gt;

&lt;p&gt;Here a good definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;magic methods will never directly be called by the programmer – actually, PHP will call the method 'behind the scenes'. This is why they are called 'magic' methods – because they are never directly called, and they allow the programmer to do some pretty powerful things.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are in total 15 magic methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__destruct&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__call&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__callStatic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__isset&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__unset&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__wakeup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__set_state&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__clone&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__debuginfo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you've done some Object Oriented Programming with PHP, you will certainly recognise the &lt;code&gt;__construct&lt;/code&gt; method which is also a magic method. So you've been using magic methods all along!&lt;/p&gt;

&lt;p&gt;You will also notice that all the magic methods are prefixed with &lt;code&gt;__&lt;/code&gt; as a convention.&lt;/p&gt;

&lt;p&gt;Today, we will not dive in all of them, but only the interesting ones used across the Laravel codebase. If you are interested by others, feel free to check the documentation below 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.php.net/manual/fr/language.oop5.magic.php" rel="noopener noreferrer"&gt;PHP: Méthodes magiques - Manual&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  How Laravel uses magic methods
&lt;/h1&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;__get()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The models in Laravel are really special. They don't store the attributes data as direct attributes of the class but in an attribute &lt;code&gt;protected $attributes&lt;/code&gt; , which is an associative array of all the data the model is holding.&lt;/p&gt;

&lt;p&gt;Let see the differences between a simple PHP class and a Laravel model accessing attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * A normal user class in PHP (without Laravel) will be just a class with the said attributes
 */&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Will return 'Alice'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Database\Eloquent\Model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * A user class in Laravel
 */&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LaravelUser&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * Note that we store all the attributes data in one and single array
     */&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'firstName'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$laravelUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LaravelUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$laravelUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Will return 'Alice' as well&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that both PHP and Laravel classes above act exactly the same. Nevertheless, in the case of Laravel, the attributes are not stored like in plain PHP and instead all the attributes are centralised in one attribute named &lt;code&gt;$attributes&lt;/code&gt;. Still we manage to access the right data but how ?&lt;/p&gt;

&lt;p&gt;This is all possible because of the &lt;code&gt;__get&lt;/code&gt; magic method. Let's try to implement our own simplify version of this in an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * We declare the attributes that same way as in Laravel
     */&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'firstName'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * The function __get receive one parameter
     * which will be the name of the attribute you want to access
     * in this case $key = "firstName"
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Will return 'Alice'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We did it! 🎉&lt;/p&gt;

&lt;p&gt;We need to note that the magic method &lt;code&gt;__get&lt;/code&gt; will only be called when no attribute with the matching name are found in the class. It's sort of a fallback method called when PHP doesn't find the asked attribute in the class. So in the example below, the magic method &lt;code&gt;__get&lt;/code&gt; will not be called at all.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Bob'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'firstName'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * Will return 'Bob' as the attribute exists in the class
 * so the magic method __get doesn't get call in this case
 */&lt;/span&gt;
&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is way more things happening behind the scene. If you want to dig more how exactly Laravel's models uses &lt;code&gt;__get&lt;/code&gt; you can check the source code below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/laravel/framework/blob/0207d73d7daae2f5902a76136780324b0cdfbb1c/src/Illuminate/Database/Eloquent/Model.php#L1519" rel="noopener noreferrer"&gt;laravel/framework&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;__set()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The magic method &lt;code&gt;__set&lt;/code&gt; is used when the attribute trying to be set is not declared in the class. Let's see the difference again between a normal PHP class and a model in Laravel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Bob'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Will return 'Bob'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Database\Eloquent\Model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LaravelUser&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'firstName'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$laravelUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LaravelUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$laravelUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Bob'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$laravelUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Will return 'Bob' as well&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, in this example we still try to affect the value &lt;code&gt;Bob&lt;/code&gt; to an attribute that doesn't really exist in the class but lives inside the attribute &lt;code&gt;$attributes&lt;/code&gt; . Let's try to implement that exact behaviour with the magic method &lt;code&gt;__set&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'firstName'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * The magic method __set receives the $name you want to affect the value on
     * and the value
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Bob'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * As we don't have the __get magic method define in this example for simplicity sake,
 * we will access the $attributes directly
 */&lt;/span&gt;
&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'firstName'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Will return 'Bob'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here we go! We successfully implemented the basic usage of the &lt;code&gt;__get&lt;/code&gt;  and &lt;code&gt;__set&lt;/code&gt; magic methods in Laravel! And all it took was a few lines of code!&lt;/p&gt;

&lt;p&gt;Keep in mind that those magic methods are as simple as possible without entering into too much details as there is more to it than just those use cases, if you are curious about how exactly it's working I invite you to dive into the code yourself for some exploring! (also feel free to hit me up on Twitter if you have any questions)&lt;/p&gt;

&lt;p&gt;Again, if you want to dig more, here the link to the source code&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/laravel/framework/blob/0207d73d7daae2f5902a76136780324b0cdfbb1c/src/Illuminate/Database/Eloquent/Model.php#L1531" rel="noopener noreferrer"&gt;laravel/framework&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's move on to the last and most interesting case! 🙌&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;__call()&lt;/code&gt;  &amp;amp;  &lt;code&gt;__callStatic()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;__call&lt;/code&gt; is executed when a method was called but not found in a specific class. In Laravel, this magic method is what is making macros possible in PHP.&lt;/p&gt;

&lt;p&gt;I'm not going into all the details on macro but if you interested here is a good article explaining how you can use them in your Laravel application 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tighten.co/blog/the-magic-of-laravel-macros" rel="noopener noreferrer"&gt;The Magic of Laravel Macros&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try to see how we could reproduce a simple macro example in plain PHP.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Bob'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// This will throw an error as no method "fullName" has been declared.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now with &lt;code&gt;__call&lt;/code&gt; , we could define an array which will contain closures function that we will be able to add programmatically in our app as we go.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Bob'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * We initialise our macros as an empty array that we will fill
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nv"&gt;$macros&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * We define this method to add new macro as we go
     * the first argument will be the name of the macro we want to define
     * the second will be a Closure function that will be executed when calling the macro
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;addMacro&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$macro&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nv"&gt;$macros&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$macro&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * "__call" receives two parameters,
     * $name which will be the name of the function called in our case "fullName"
     * $arguments which will be all the arguments passed in the function in our case it'll just be an empty array as we can't pass any arguments in our function
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cd"&gt;/**
         * We retrieve the macro with the right name
         */&lt;/span&gt;
        &lt;span class="nv"&gt;$macro&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nv"&gt;$macros&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="cd"&gt;/**
         * Then we execute the macro with the arguments
         * Note: we need to bind the Closure with "$this" before calling it to allow the macro method to act in the same context
         */&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;call_user_func_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$macro&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;bindTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nv"&gt;$arguments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NormalUser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// This will still break as we didn't define the macro "fullName" yet and the method "fullName" doesn't exist either&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * We add the macro function "fullName"
 */&lt;/span&gt;
&lt;span class="nc"&gt;NormalUser&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;addMacro&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'fullName'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nv"&gt;$normalUser&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Now, returns "Alice Bob"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Macros are a little more complex than that but again, we manage to create a simple working version of the macros by using &lt;code&gt;__call&lt;/code&gt; magic method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;__callStatic&lt;/code&gt; works exactly the same as &lt;code&gt;__call&lt;/code&gt; but for static methods.&lt;/p&gt;

&lt;p&gt;Again if you want to dig a little bit more on your own, here is the &lt;code&gt;Macroable&lt;/code&gt; trait source code&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/laravel/framework/blob/e6c8aa0e39d8f91068ad1c299546536e9f25ef63/src/Illuminate/Support/Traits/Macroable.php" rel="noopener noreferrer"&gt;laravel/framework&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final take
&lt;/h2&gt;

&lt;p&gt;Here you go Ladies and Gentlemen, while it is somewhat true that Laravel feels &lt;em&gt;magic&lt;/em&gt; when you first start using it, by looking into the source code itself, you can understand how the "magic" operate behind the scene.&lt;/p&gt;

&lt;p&gt;Like magic in real life, nothing really happens without an explanation and that's even truer in code. There is always a line of code doing the work somewhere, you just have to find it.&lt;/p&gt;

&lt;p&gt;I encourage you to dig more into Laravel to make the magic disappear and don't forget to share your nuggets with me on Twitter!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This blog post was originally posted on &lt;a href="https://stevenyung.me/laravel-greatest-trick-revealed-magic-methods/" rel="noopener noreferrer"&gt;my personal blog&lt;/a&gt;, please give it a hug 🤗&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Special Thanks: &lt;a href="https://twitter.com/aukraiser" rel="noopener noreferrer"&gt;Samuel&lt;/a&gt;, &lt;a href="https://github.com/veronj" rel="noopener noreferrer"&gt;Jean-Baptiste&lt;/a&gt; and &lt;a href="https://twitter.com/Jessica_thlmns" rel="noopener noreferrer"&gt;Jessica&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>magic</category>
    </item>
    <item>
      <title>Be good at one thing, not average at everything</title>
      <dc:creator>Steven Yung</dc:creator>
      <pubDate>Wed, 10 Jan 2018 13:20:50 +0000</pubDate>
      <link>https://forem.com/excessivecoding/be-good-at-one-thing-not-average-at-everything-1bf1</link>
      <guid>https://forem.com/excessivecoding/be-good-at-one-thing-not-average-at-everything-1bf1</guid>
      <description>&lt;p&gt;&lt;em&gt;(Credit to &lt;a href="https://goo.gl/EHsSdH" rel="noopener noreferrer"&gt;Jessica&lt;/a&gt; for the photo)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The web is constantly evolving and improving over time and there is already so much to learn and much more as time goes by.&lt;/p&gt;

&lt;p&gt;For any newcomers, this is pretty scary, and it becomes quickly overwhelming.&lt;/p&gt;

&lt;p&gt;I started as a working student in a web agency 2 years ago with no concrete experience and not a clue how real web development was done. My first day, I was excited to discover new things but mostly afraid by the daunting task ahead of me.&lt;/p&gt;

&lt;p&gt;The worst part (or best, depending on whether you are a half-full or half-empty glass kind of person) is that the more you discover about the various aspects of web development, the more you realize how little you actually know.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trying to learn everything
&lt;/h2&gt;

&lt;p&gt;One of the most common pitfalls that new developers fall into is trying to understand and do a lot because, let's admit it, everybody seems ahead of you and the level you have to reach to be "good" seems further and further away.&lt;/p&gt;

&lt;p&gt;As a developer, it’s pretty easy to lose focus and get caught on "&lt;a href="https://www.quora.com/Whats-shiny-object-syndrome" rel="noopener noreferrer"&gt;The Shiny Object Syndrome&lt;/a&gt;". The grass is always greener on the other side of the fence and programming is no exception as there is always new ways to build stuff, one more exciting than the other.&lt;/p&gt;

&lt;p&gt;During my first year, I started learning on my free time to improve as a Symfony developer, but I lost my focus trying to play with everything I could get my hands on. From C compiler to Node.js. I learned everything but Symfony because anything else was new and more exciting.&lt;/p&gt;

&lt;p&gt;And I could have gone on, and on, and on, and on for as long as new trends were popping out on Github. I spent countless hours watching, reading, learning and breathing code.&lt;/p&gt;

&lt;p&gt;In hindsight,  I spread my focus too thin. Learning is never a waste of time, of course. It's always good to try out new technology and be aware of the new trending things in your ecosystem at the moment.&lt;/p&gt;

&lt;p&gt;However, you really start pushing your limit and the limit of your tools when you focus on one thing: &lt;strong&gt;one&lt;/strong&gt; framework, &lt;strong&gt;one&lt;/strong&gt; language, &lt;strong&gt;one&lt;/strong&gt; project, etc. And you will get deeper knowledge along the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  From average at everything to good at one thing
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;"Success demands singleness of purpose. You need to be doing fewer things for more effect instead of doing more things with side effects."&lt;/em&gt; - &lt;a href="https://www.audible.fr/pd/Ang-Business/The-ONE-Thing-Livre-Audio/B00FOXX83O/ref=a_search_c4_1_1_srImg?qid=1515172427&amp;amp;sr=1-1" rel="noopener noreferrer"&gt;The ONE Thing&lt;/a&gt; by Gary Keller&lt;/p&gt;

&lt;p&gt;(I highly recommend reading &lt;a href="https://www.audible.fr/pd/Ang-Business/The-ONE-Thing-Livre-Audio/B00FOXX83O/ref=a_search_c4_1_1_srImg?qid=1515172427&amp;amp;sr=1-1" rel="noopener noreferrer"&gt;The ONE Thing&lt;/a&gt; by Gary Keller not your usual programming book but incredibly interesting and eye-opening)&lt;/p&gt;

&lt;p&gt;During my last year as a working student, I started shifting the way I was seeing "improvement" from "The more technologies and languages I know, the better I am" mentality to "The better I understand the technologies and languages I already know, the better I am".&lt;/p&gt;

&lt;p&gt;So I focused on what little I already knew: back-end development in PHP. Back-end development with &lt;a href="https://laravel.com" rel="noopener noreferrer"&gt;Laravel&lt;/a&gt; to be precise.&lt;/p&gt;

&lt;p&gt;Soon after understanding the basics and getting a general feel of the framework, I focused more on building things with my current knowledge instead of learning the next trending thing on &lt;a href="https://github.com" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I started building stuff, useful stuff (at least, useful for me) and that's when I started realizing, I kept learning new things, but instead of learning to use new tools, I learned to get the best out of those I already had.&lt;/p&gt;

&lt;p&gt;I've improved my code reusability, started using design patterns, implemented core concepts like &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="noopener noreferrer"&gt;DRY&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)" rel="noopener noreferrer"&gt;SOLID&lt;/a&gt; in my codebase, learned about &lt;a href="https://en.wikipedia.org/wiki/Test-driven_development" rel="noopener noreferrer"&gt;Test-Driven-Development&lt;/a&gt;, etc.&lt;/p&gt;

&lt;p&gt;Overall, I became a better PHP back-end developer but also a better developer in general. Because, when you learn and become fluent with your tools, that's when you start learning programming beyond the framework or language and start implementing general concept which will be universal to any technology you know.&lt;/p&gt;

&lt;p&gt;As I made my way and became an okay-ish &lt;a href="https://laravel.com" rel="noopener noreferrer"&gt;Laravel&lt;/a&gt; developer, I also started my own business as a Laravel freelance developer, and to focus on what I knew and keep improving, I would work on the back-end of any project and send any front-end or not suitable work to another freelancer.&lt;/p&gt;

&lt;p&gt;That way, I really focused my attention on one thing at a time while still moving my freelance skill forward and I wasn't limited to my own knowledge to work on a project. I could keep doing and improve what I knew while learning some valuable lessons outside of programming like project management and remote teamwork.&lt;/p&gt;

&lt;h2&gt;
  
  
  An incremental approach to learning
&lt;/h2&gt;

&lt;p&gt;When you reach the point where you feel confident about your ability to use your current tools, you can start incrementally implementing new skill into your developer portfolio, but not a random skill, in the best case scenario a skill that will complete yours, the goal remains the same, "become better with what you already know" and you can achieve that with complementary skill.&lt;/p&gt;

&lt;p&gt;The final goal is to always learn with the purpose of improving current knowledge to improve as a whole. And with this approach, you can easily start using your current stack and implement what your new tool is.&lt;br&gt;
For me, it was learning &lt;a href="https://vuejs.org" rel="noopener noreferrer"&gt;Vue.js&lt;/a&gt; to work toward a more full-stack developer profile for my freelance activity.&lt;br&gt;
For back-end developer, it could be adding server administration to his portfolio to understand how to implement a full web app on his own.&lt;br&gt;
For a front-end developer, setup and use &lt;a href="http://postcss.org" rel="noopener noreferrer"&gt;PostCSS&lt;/a&gt; to improve code quality and his workflow.&lt;/p&gt;

&lt;p&gt;The options are endless and the goal is always the same: improve your strength toward a specific goal.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Like this article? Want to see what I'm up to? You can follow me on &lt;a href="https://goo.gl/fJiHjJ" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, &lt;a href="https://goo.gl/Y77U6M" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt; and &lt;a href="https://goo.gl/m7WMo1" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Special thanks to: &lt;a href="https://goo.gl/2Ezjbz" rel="noopener noreferrer"&gt;Abdoulaye&lt;/a&gt;, &lt;a href="https://twitter.com/AukRaiser" rel="noopener noreferrer"&gt;Samuel&lt;/a&gt; and &lt;a href="https://github.com/veronj" rel="noopener noreferrer"&gt;Jean-Baptiste&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>learning</category>
      <category>tools</category>
      <category>devtips</category>
      <category>advice</category>
    </item>
  </channel>
</rss>
