Posts tagged with coding

Join 9,500+ smart developers

Every month I share what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

No spam. Unsubscribe anytime. You can also follow me on X.

Using EditorConfig

Frederick Vanbrabant recorded a new cool video, this time on EditorConfig.

EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs. The EditorConfig project consists of a file format for defining coding styles and a collection of text editor plugins that enable editors to read the file format and adhere to defined styles. EditorConfig files are easily readable and they work nicely with version control systems.

Read more

Enabling PHP method chaining with a makeshift pipe operator

Sebastiaan Luca, a freelance Laravel developer from Antwerp, coded up a couple of functions that mimic a pipe operator.

An interesting RFC proposal by Sara Golemon submitted in April 2016 suggested the use of a pipe operator to enable method chaining for any value and method. Yet as of today, it's still being discussed and there's no saying if it will ever make its way into PHP. So in the meantime, here's a solution!

https://blog.sebastiaanluca.com/enabling-php-method-chaining-with-a-makeshift-pipe-operator

Let's hope a real pipe operator will land someday in PHP.

Read more

Pragmatic coding

Stefan Koopmanschap argues that, besides writing beautiful code and using kickass frameworks and libraries, we should be able to do some quick and dirty coding as well.

Can you write me a simple script that fetches some information from an RSS feed and displays the titles? Like, just write me that script in a couple of minutes. I don't care about tests, quality, etc. Just get me the information, quickly.

http://leftontheweb.com/blog/2017/01/04/pragmatic_coding/

Read more

No Time for a Taxicab

Gary Hockin posted a video with his attempt in solving the Day 1 challenge of http://adventofcode.com/. The video not only shows how he solved the problem codewise but also demonstrates some nice features of PHPStorm.

Mistakes and all, I attempt to code day 1 part 1 of the Advent of Code challenges you can find at http://adventofcode.com.

I deliberately didn't overly edit, or over complicate the video as I'm trying to get them done as fast as possible, if you like this I'll do some more!

I know some people will say "Waaa you should have done it like this!", or "Why didn't you use library $x", well I didn't so get over it. I'm also worried this gives away far too much about my coding quality and how lazy I am, but such is life.

Read more

Method overloading is possible in PHP (sort of)

PHP does not support method overloading. In case you've never heard of method overloading, it means that the language can pick a method based on which parameters you're using to call it. This is possible in many other programming languages like Java, C++.

So, under normal circumstances, you can't do this in PHP:

class Foo
{
   function bar(A $baz)
   {
      ...
   }

   function bar(B $baz)
   {
      ...
   }
}

However, with some clever coding, Adam Wathan made a trait, aptly called Overloadable, that makes method overloading possible. It works by just accepting any parameters using the splat operator and then determining which of the given functions must be called according to the given parameters.

Let's rewrite that example above using the Overloadable trait.

class Foo
{
    use Overloadable;

    function bar(...$arguments)
    {
        return $this->overload($arguments, [
            function (A $baz) {
               $this->functionThatProcessesObjectA($baz);
            },
            function (B $baz) {
               $this->functionThatProcessesObjectB($baz);
            },
        ]);
    }
}

Pretty cool stuff. In a gist on GitHub Adam shares a couple of examples, the source code of the trait and the tests that go along with it. Check it out!

https://gist.github.com/adamwathan/120f5acb69ba84e3fa911437242796c3

Read more