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
.
- Source: Fastest way to load serialized data : PHP
- brick/varexporter: A powerful alternative to var_export(), which can export closures and objects without __set_state()
#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.
- Basic sitemap crawler to warm up a full page cache
- eliashaeussler/cache-warmup: 🔥 Composer package to warm up caches of pages located in XML sitemaps
#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.
-
VCS in composer.json:
- Defines custom repository sources for packages
- Allows using forked or private repositories
-
Installing custom packages:
- Add to "require" section in composer.json
- Run composer update or require command
-
Installing forked packages:
- Add forked repo to "repositories" section
- Specify package in "require" section
-
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"
-
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
- ezyang/htmlpurifier: Standards compliant HTML filter written in PHP
- kesar/HTMLawed: a highly customizable PHP script to sanitize / make (X)HTML secure against XSS attacks, so users can edit HTML without risk of your site getting compromised by evildoers.
- afuafuyo/html-filter-php: filter html for php