Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
mindplay-dk / session-life-cycle.md
Last active March 28, 2024 19:52
Complete overview of the PHP SessionHandler life-cycle

This page provides a full overview of PHP's SessionHandler life-cycle - this was generated by a set of test-scripts, in order to provide an exact overview of when and what you can expect will be called in your custom SessionHandler implementation.

Each example is a separate script being run by a client with cookies enabled.

To the left, you can see the function being called in your script, and to the right, you can see the resulting calls being made to a custom session-handler registed using session_set_save_handler().

@mindplay-dk
mindplay-dk / factory.php
Created March 24, 2024 16:53
factory.php
<?php
class UserProvider
{
public function __construct(
#[Service("user.cache-path")]
public readonly string $cache_path
) {}
#[Service("user.cache")]
@mindplay-dk
mindplay-dk / css-capture.js
Last active March 25, 2024 12:38
Capture CSS rules being used on the page
/**
* This script captures all CSS rules that are in use on the current page.
*
* - Paste the script into the browser console.
* - Run `collector.rules` to get a list of CSS rules in use.
* - As a diagnostic, run `collector.rulesWithComments` to get a list of CSS rules with comments.
*/
var collector = (() => {
const rulesInUse = new Set();
@mindplay-dk
mindplay-dk / waitForElement.js
Last active March 25, 2024 10:17
waitForElement function (wait for element matching selector)
let animationCounter = 0;
export function waitForElement(selector) {
return new Promise((resolve) => {
const elem = document.querySelector(selector);
if (elem) {
resolve(elem); // already in the DOM
}
@mindplay-dk
mindplay-dk / README.md
Last active March 16, 2024 12:14
ISO 3166-2 Country and State/Region Tables for MySQL
@mindplay-dk
mindplay-dk / find-var.js
Last active February 29, 2024 09:23
Recursively search the global namespace (window) for a variable with a given name
function findVar(varName) {
let seen = new Map();
function search(obj, prefix = "") {
if (seen.has(obj)) {
return;
}
seen.set(obj, true);
@mindplay-dk
mindplay-dk / reactive.js
Last active January 27, 2024 13:51
reactive JS 🤔 everything is a signal? 🤷‍♂️
/*
I know this is probably bonkers and there must be a million reasons this wouldn't
actually work, but hear me out... what if variables were reactive by default?? 😏
*/
function fetchTodayFortune() {
return fetch("https://api.example.com/fortune")
.then((response) => response.json())
@mindplay-dk
mindplay-dk / php-upgrades.md
Last active December 28, 2023 16:46
PHP upgrades

Upgrading PHP

Guidelines for upgrading the minimum PHP version requirements of packages and projects.

This isn't meant to be an exhaustive guide to upgrading, but as a checklist for the most important upgrades.

PHP 5.3

The first version to support namespaces - any relevant PHP packages/projects usually have this version as the minimum requirement, so this document won't concern itself with upgrades prior to that.

@mindplay-dk
mindplay-dk / ubuntu.md
Last active December 15, 2023 07:37
Ubuntu on Windows

Introduction

⚠️ I am no longer actively maintaining this. ⚠️

With the Windows 10 Creators Update comes an awesome new opportunity to run a lighweight Ubuntu Linux environment as a subsystem of Windows. This is officially known as Windows Subsystem for Linux (WSL) or Bash on Windows.

This is great news for PHP developers using Windows - we can now get access to native Linux builds of PHP, PECL extensions, ImageMagick, NGINX and other server components and tools, without having to migrate to a Linux desktop environment!

In this guide, we'll get you set up with WSL itself, a working PHP 8.2 environment with OpCache, XDebug and task/terminal integration with Php Storm, and working NGINX configuration - as well as various tools, including PEAR/PECL, Composer and Node.JS.

@mindplay-dk
mindplay-dk / reflection-bench.php
Created August 15, 2012 12:40
A benchmark of reflection API performance in PHP
<?php
/**
* Benchmark: Reflection Performance
*
* Conclusion: there is no performance-gain from caching reflection-objects.
*/
define('NUM_TESTS', 10);