Why objects (usually) use less memory than arrays in PHP

Because the properties for the object are predefined PHP no longer has to store the data in a hashtable, but instead can say that `$foo` is property 0, `$bar` is property 1, `$baz` is property 2 and then just store the properties in a three-element C array.

This means that PHP only needs one hashtable in the class that does the property-name to offset mapping and uses a memory-efficient C-array in the individual objects. Arrays on the other hand need the hashtable for every array.

https://gist.github.com/nikic/5015323

Read more

Join 9,500+ smart developers

Get my monthly newsletter with 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.

HTTP based image manipulations

Glide is a wonderfully easy on-demand image manipulation library written in PHP. It’s straightforward API is exposed via HTTP, similar to cloud image processing services like Imgix and Cloudinary. Glide leverages powerful libraries like Intervention Image (for image handling and manipulation) and Flysystem (for file system abstraction).
http://glide.thephpleague.com/

It comes with the ability to secure image URL's using a private signing key. I'm a bit worried about flooding the server but the owner of the package states that

... the cache filesystem + caching in league/flystem (with Redis/Memcache) should (at least this is the case in my testing) result in a speedy response...
There's also a risk that over time your cache will become gigantic. The maintainers of the package are already discussing possible solutions.

Read more

Automating Laravel deployments using Capistrano

Capistrano is a server automation and deployment tool. It makes a whole bunch of servers simultaneously perform actions. This could be deploying our application, or clearing their cache.

Capistrano tells each of our servers to pull the latest branch from Git, perform any necessary build steps, and deploy the application on itself. Setting up this process is what we'll be detailing.

https://www.airpair.com/laravel/posts/automating-laravel-deployments-using-capistrano

If you want to use something simpler, take a look at Deployer.

Read more

PHPCI: a CI tool specifically designed for PHP

PHPCI is a free and open source continuous integration tool specifically designed for PHP. Built with simplicity in mind and featuring integrations with all of your favourite testing tools, we've created the very best platform for testing your PHP projects.
https://www.phptesting.org/

Over at Sitepoint Peter Nijssen wrote a good overview of the offered functionality.

I've been playing around with the self-hosted version. It comes with a bunch of plugins that are installed by default.

I can recommended it if you, like me, are starting out with CI.

Read more

Easy deployments for php projects

Let's talk a bit about deployments.

All the new projects I work on are hosted on servers that are provisioned by Forge. Forge has a quick deploy feature that allows you to pull the lastest master-commit onto the server, install composer dependencies, run migrations and so on.

It has two main drawbacks:

  • you can't see any output of the deployment script in your terminal
  • you can't do some stuff locally on your computer
To solve these points I'm switching from Forge's quick deploy feature to Deployer.
Deployer is a deployment tool written in PHP, it's simple and functional. Deploy your code to all servers you want, it supports deploy via copy, or via VCS (like git), or via rsync. Run your tasks on all your servers, or use our recipes of common tasks for Symfony, Laravel, Zend Framework and Yii.
You can install Deployer via composer. To use Deployer a deploy.php file must be added to your project. This file describes your deployment process. No worries, you can write it in php using a very clean and readable syntax. The documention on how to write the deploy.php-file is a bit vague at this point, but you'll get a good idea of how things work by taking a look at my deploy.php-file.

To run deployer you'll use this command:

[code lang="bash"] dep



I've made a bash alias that uses the name of the git branch you're currently on:

[code lang="bash"]
alias deploy='dep deploy $(git symbolic-ref --short HEAD)'

Now deploying a branch is as easy as just typing "deploy" when standing on a git repo in your terminal. This is a much nicer way to handle deployments. What I really like is that, now a deploy.php stored in the git repo, the deployment process is also versioned.

If you find Deployer doesn't meet your needs, Rocketeer, Phing and Capistrano are some more advanced alternatives. Envoy is nice too, but I haven't found a way to run stuff locally.

Read more

Automatically reject packages with known security vulnerabilities

This package ensures that your application doesn't have installed dependencies with known security vulnerabilities.

...

The checks are only executed when adding a new dependency via composer require or when running composer update: deploying an application with a valid composer.lock and via composer install won't trigger any security versions checking.

https://github.com/Roave/SecurityAdvisories

Awesome idea! It works by leveraging the "conflict"-property in the composer.json-file of the package.

Read more

An overview of the command bus

A command bus helps separating business logic from all the stuff that needs to be done to communicate over the web.

Shawn McCool wrote up a good overview about the subject.

Think of a typical controller. Inside, you might find interactions with the model, form validation, and response generation; which includes view template rendering or redirect header generation. Some of these actions are related directly to the web, some of them directly to the core of your application.
http://shawnmc.cool/command-bus

Read more

A Laravel webshop original

by Freek Van der Herten – 3 minute read

Earlier today Spatie, the company where I work, launched a webshop polkadots.be. When the project started one of the first choices that we had to make was if we were going to use an existing solution or build our own custom solution. For this project we took an extensive look at some existing…

Read more

Excel exports

Evan Miller makes the case for offering xls-exports instead of csv-exports.

Most people of your website’s users are also Excel users. When they export their data as CSV, they’ll probably just bring it into Excel first to have a look around. You should probably offer an explicit XLS export, and take it seriously.
http://www.evanmiller.org/please-offer-an-excel-export-option.html

An xls-file is capable of things that 'll never be possible in csv:

  • freezing the header row
  • formatting important cells
  • multiple sheets in one file
  • specify numer formatting
  • formulas
In the projects I've been working on I've been doing this for a couple of months. A nice Laravel package you can use is Laravel Excel.

This is how you export all values from a given repository that returns Eloquent models:


Excel::create($pathToFile, function($excel) {

    $excel->sheet('The sheet name', function($sheet) {
        $sheet->freezeFirstRow();

        $sheet->cells('A1:Z1', function($cells) {
            $cells->setFontWeight('bold');
            $cells->setBorder('node', 'none', 'solid', 'none');
        });

        $sheet->fromModel($this->yourOwnRepository->getAll());
    });

})->export('xls');

The result is an excel-file with the header row frozen and underlined and it's values in bold.

Read more

Functional programming in PHP

Almost a year ago Igor Wiedler wrote three articles on his blog about the state of functional programming in PHP.

The first article explores iteration. You'll learn to turn this


$admins = [];
foreach ($users as $user) {
    if (is_admin($user)) {
        $admins[] = $user;
    }
}

into this


use function iter\filter;
$admins = filter('project\user\is_admin', $users);

In the second one he explains a very nice way to traverse an associative array. How you currently do it:


$baz = (isset($data['foo']['bar']['baz'])) ? $data['foo']['bar']['baz'] : null;

How you'll do it in the future:


use function igorw\get_in;
$baz = get_in($data, ['foo', 'bar', 'baz']);

The final article shows you a nice syntax to handle objects that return null-values.

All the articles mention libraries that you can use in your code today.

Read more

Inside Composer's speed boost

If you've been following the news, you'll have noticed that yesterday Composer got a bit of a speed boost. And by "bit of a speed boost", we're talking between 50% and 90% reduction in runtime depending on the complexity of the dependencies. But how did the fix work? And should you make the same sort of change to your projects? For those of you who want the TL/DR answer: the answer is no you shouldn't.
http://blog.ircmaxell.com/2014/12/what-about-garbage.html

Read more

Add Two-Factor authentication to your Laravel application

Authy makes it easy to integrate two-factor authentication into your existing Laravel application. It's user-end implementation is versitile, offering support for both smart-phones and standard phones. Furthermore, implmentation with its API is easy and seamless. Finally, its analytics and data tracking can you provide insights into your users and how they interact with your application's authentication system.

This tutorial assumes that you have an Authy developer account (which can be created here ), an application (either testing or production), and your application's API key.

http://blog.enge.me/post/installing-two-factor-authentication-with-authy

Read more