2023-11-13 #php #laravel

Remove falsy values from a Laravel collection or array in PHP

The native array_filter() in PHP and collect()->filter() in Laravel also work without providing a filter callback.

array_filter([0, 1, '', 'a', false, true, []]);
// [1, 'a', true]
 
collect([0, 1, '', 'a', false, true, []])->filter();
// [1, 'a', true]

If you don't provide a callback, PHP will remove all empty values from the array.


2023-08-02 #php #go / themsaid.com

Mohamed Said on the synergy between PHP & Go

I enjoyed Mohamed's post on using PHP and Go to have the best of both worlds.

By employing a polyglot architecture, we get the best of both worlds. PHP provides the development speed required to compete in a hyper-growth market, while Go provides more efficient resource utilization.

Another good quote from the introduction of his PHP to Go course:

PHP may be slow and memory hungry when compared to a compiled language, but that's not a result of a bad design.

It's like this by design because of all the choices it makes on your behalf to conceal complexity.

I also recommend his post on Twitter about the business decisions behind cutting costs on infrastructure.


2023-07-17 #php #php-unit #testing

Beware of PHPUnit data providers with heavy setup methods

Data providers can be a perfect fit to assert a lot of expectations without writing a full test for each, but they can slow down your tests unnecessarily.

Read more


2023-06-27 #php #php-wishlist

PHP wishlist: Nested properties

Next on my PHP wishlist are nested properties. This idea is less realistic than others, it's more me thinking out loud. I don't have a good syntax proposal for this, and I'm not even sure it's the best solution for my problem. But it's the best I've come up with so far.

Read more


2023-03-17 #laravel #valet #php #nginx

PHP & NGINX logs with Laravel Valet

Putting this in a blog post because I always forget.

To view PHP logs from Laravel Valet:

open ~/.config/valet/Log/php-fpm.log

To view NGINX logs from Laravel Valet:

open ~/.config/valet/Log/nginx-error.log

2023-03-06 #php #laravel / freek.dev

How we keep our Laravel packages customizable at Spatie

Freek shares a few patterns we employ to let developers override behaviour in our packages.

One of the ways we keep maintenance burden low is by making our packages customizable. In this blog post, I'd like to cover some of our best tips to make a Laravel package easy to customize. Some of these tips will apply to regular projects as well.


2023-02-16 #php #php-wishlist

PHP wishlist: The pipe operator

Is it weird to have a favorite operator? Well, the pipe operator |> is mine. Not only does it look cool, it opens a world of possibilities for better code.

Unfortunately, it's not available in any of the languages I use on a daily basis. There are proposals to add it to PHP and JavaScript, but we're not there yet. I'd like to expand on why I think the pipe operator would be a valuable addition to the language from a PHP developer's perspective.

Read more


2022-12-08 #php / verraes.net

Deterministic bliss in static methods

Static methods tend to have a bad reputation in PHP, but I believe (stateless) static methods are underused. In static functions, there's no internal state to take into account. Calculator::sum(1, 2) only depends on its input, and will always return 3.

While researching for another post, I came across an article from Mathias Verraes that already says everything I wanted to say.

It is stateless, it is free of side effects, and as such, it is entirely predictable. You can call the exact same function with the exact same argument as often as you like, and you will always get the exact same result back.


2022-09-15 #php

Named arguments

I shiver at the sight of a function packed with too-many-to-read-at-a-glance arguments without a description.

Read more


2022-09-05 #programming #php

Granular interfaces

A few weeks ago a spec change for an application we're working on forced us to refactor part of the codebase. It was food for thought about the flexibility granular interfaces provide, and choosing the right abstraction at the right time. This is a short writeup on the thought process we went through as we updated our logic to support a new feature now and allow more options in the future.

Read more


2021-07-22 #laravel #php

Use Blink to execute something once and only once

Our Blink package is marketed as a caching solution to memoize data for the duration of a web request. Recently, we came upon another use case for the package: to execute something once and only once.

Read more


2020-10-01 #spatie #laravel #php #javascript

The revamped Spatie guidelines

I created the original Spatie guidelines site three years ago. Last month, we consolidated a few of our subsites to our main spatie.be site, including the guidelines.

Read more


2018-11-30 #php #markdown

Highlighting code blocks with league/commonmark

Since the first iteration of my blog—some time around 2016—I've used highlight.js to highlight code blocks. With highlight.js being so popular, I never really second guessed the idea. It was a given to use JavaScript.

A few weeks ago, TJ Miller introduced me to highlight.php by writing a Parsedown extension for it. Highlight.php does the exact same thing as highlight.js: they both add tags and classes to your code blocks, which enables them to be highlighted with CSS. The difference is, highlight.php does this on the server.

Read more


2018-03-11 #php #javascript #ssr

Server side rendering JavaScript from PHP

Server side rendering is a hot topic when it comes to client side applications. Unfortunately, it's not an easy thing to do, especially if you're not building things in a Node.js environment.

I published two libraries to enable server side rendering JavaScript from PHP: spatie/server-side-rendering and spatie/laravel-server-side-rendering for Laravel apps.

Let's review some server side rendering concepts, benefits and tradeoffs, and build a server renderer in PHP from first principles.

Read more


2018-02-05 #laravel #php #blade

Blade component aliases in Laravel 5.6

Laravel 5.6 adds the ability to register alias directives for Blade components. Let's review some background information and examples.

Read more