PHP

Fastest way to load serialized data

Instead of load a serialized object every time and unserialize it, write it to the filesystem as php file and include() it.

Depending on the case, this might be premature optimization.

$s = serialize(['a' => 1, 'bla', 'b'=>['a'=> false]]);
$data = '<?php return ' . var_export(unserialize($s), true) . ';';
// check if data changed then write it
file_put_contents('temp_data.php', $data);
$out = include('temp_data.php');
var_dump($out);

A similar technique is used in template engines, where the {{ $content }} of template.blade.php gets rendered as <?= $content ?> in randomhash.php.

#optimization #premature-optimization

Cache Warm Up

Two things in programming are complicated: Caching and yadda yadda... I know. When caching frontend pages on a low frequented site, someone has to wait for the page to be cached after it expired (assuming pages are cached with an expiry date).

For that, warming up the cache can be appropriate. Set up a cronjob that requests the sitemap.xml and sends a HEAD request to every page in there.

#cache

Using a Custom Branch of a Library in Composer

To utilize your own branch of a library, incorporating fixes or modifications, follow these steps to update the composer file:

{
    "require": {
-       "kevinrob/guzzle-cache-middleware": "^3.3",
+       "kevinrob/guzzle-cache-middleware": "dev-feature-flysystem2",
        "league/flysystem": "^2.1"
    },
+   "repositories": [
+       {
+           "type": "vcs",
+           "url": "https://github.com/marcus-at-localhost/guzzle-cache-middleware"
+       }
+   ]
}

In this code snippet, "dev-feature-flysystem2" refers to a branch named feature-flysystem2 in the repository located at "https://github.com/marcus-at-localhost/guzzle-cache-middleware".

The "type": "vcs" entry indicates that the specified URL represents a Version Control System (VCS) repository, such as Git, Mercurial, or SVN. This configuration allows Composer to directly retrieve the package from the VCS repository when installing dependencies.

  1. VCS in composer.json:

    • Defines custom repository sources for packages
    • Allows using forked or private repositories
  2. Installing custom packages:

    • Add to "require" section in composer.json
    • Run composer update or require command
  3. Installing forked packages:

    • Add forked repo to "repositories" section
    • Specify package in "require" section
  4. Version constraints:

    • "dev-master": Latest commit on master branch
    • Specific versions: "1.2.3", "^1.2", "~1.2", "1.2.*"
    • Branch-specific: "dev-branchname"
    • Commit-specific: "dev-master#commithash"
  5. VCS indicator:

    • "dev-*" prefix signals Composer to use VCS repository

PhpQuery

This shouldn't be used anymore, on the other side it's the most convenient tool to manipulate the DOM in PHP.

The latest PHP 8.x compatible version is my fork of a fork

{
    "require": {
        "scssphp/scssphp": "^1.0",
        "jaeger/phpquery-single": "dev-master"
    },
    "repositories": [
       {
           "type": "vcs",
           "url": "https://github.com/marcus-at-localhost/phpQuery-single"
        }

    ],
    "require-dev": {
        "kint-php/kint": "^5.1"
    }
}

Clean up HTML