Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
mindplay-dk / ReflectionParameter.md
Created October 11, 2023 10:40
PHP ReflectionParameter overview

With so many parameter modes in PHP, I wanted to know exactly what ReflectionParameter is going to return.

<?php

class Database {}

$fns = [
    function ($str) {},
    function ($str ="hello") {},
@mindplay-dk
mindplay-dk / twitter.svg
Created September 7, 2023 07:36
Twitter Logo
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mindplay-dk
mindplay-dk / ts-sql-libs.md
Last active October 14, 2023 16:14
TypeScript SQL libs

Type-safe TS SQL Libs

A list of query-builders and other SQL query and/or schema abstractions.

Libraries with no activity for 2+ years will be considered "dead", as these are not keeping up with the language or ecosystem - but they may be added and may remain on the list, if they contain different or interesting ideas.

Fluent

from(books).where(books.author.lower().like('%bob') and similar.

@mindplay-dk
mindplay-dk / psr-middleware.php
Created December 13, 2022 11:32
PSR Handlers as middleware
<?php
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
// There are essentially 3 handler/middlerware patterns:
//
@mindplay-dk
mindplay-dk / php-chatgpt.md
Created December 12, 2022 10:29
Teaching ChatGPT new PHP syntax

I was curious if we could use ChatGPT to generate a new and better manual for PHP.

This example was interesting enough that I figured it was worth posting.

It looks like ChatGPT does not know PHP 8.1 - the latest version it seems to know is PHP 8.

When asked to explain the first class Callable(...) syntax (introduced in PHP 8.1) it does explain how to generate something that is functionally similar, using e.g. fn($car) => $car->drive() rather than $car->drive(...). So it understands the concept, it just doesn't know about the new syntax.

But here's the wild part: I then proceded to explain the new syntax to it, and asked it to explain again using the new syntax - and it did it. 😮

@mindplay-dk
mindplay-dk / response.md
Last active July 16, 2022 14:45
Named registrations in dependency injection containers

In response to this answer:

The built-in dependency injection container does not support named dependency registrations, and there are no plans to add this at the moment.

One reason for this is that with dependency injection, there is no type-safe way to specify which kind of named instance you would want. You could surely use something like parameter attributes for constructors (or attributes on properties for property injection) but that would be a different kind of complexity that likely wouldn’t be worth it; and it certainly wouldn’t be backed by the type system, which is an important part of how dependency injection works.

I'm not sure what is meant by "type-safe" here?

The way service providers are provisioned in .NET today (via service providers and descriptors) is only "type-safe" in the sense that e.g. constructors specify the required types of dependencies that should be injected - th

@mindplay-dk
mindplay-dk / php-upgrades.md
Last active June 11, 2024 10:30
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 / README.md
Created September 7, 2020 13:32
Monkey-patching/polyfill detector

This script tries to detect monkey-patching, polyfills and other hacks/overrides in the browser.

Paste it into the Chrome (or Edge) console and press ENTER.

Note that this may give false positives for window.location, window.fetch and window.length - this appears to be because these properties aren't correctly reflected by the native browser implementations, but if you know how to fix that, please post a comment.

@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 / image-url.ts
Last active September 13, 2024 22:45
Rotate image preview to compensate for EXIF orientation (Javascript / Typescript)
// Based on: https://stackoverflow.com/a/46814952/283851
/**
* Create a Base64 Image URL, with rotation applied to compensate for EXIF orientation, if needed.
*
* Optionally resize to a smaller maximum width - to improve performance for larger image thumbnails.
*/
export async function getImageUrl(file: File, maxWidth: number|undefined) {
return readOrientation(file).then(orientation => applyRotation(file, orientation || 1, maxWidth || 999999));
}