boson-php/weak-types
Weak-types helpers for the Boson PHP ecosystem. Install via Composer and use alongside Boson to build desktop apps with configuration, windows, webviews, bindings, scripts, and request interception—see the Boson docs for guides and APIs.
Installation:
composer require boson-php/weak-types
Ensure your project uses PHP 8.4+ (check composer.json and server compatibility).
First Use Case: Convert a loosely typed array (e.g., API response) into a weakly typed structure:
use Boson\WeakTypes\WeakMap;
$data = ['user' => ['name' => 'John', 'roles' => ['admin']]];
$weakMap = WeakMap::from($data);
// Access values (weakly typed)
$name = $weakMap->get('user.name'); // Returns 'John' (string)
$roles = $weakMap->get('user.roles'); // Returns ['admin'] (array)
Where to Look First:
WeakMap: For key-value pairs with weak references (e.g., caching, dynamic configs).WeakSet: For unique weak references (e.g., tracking objects without strong holds).WeakType: For custom weakly typed objects (advanced use cases).Dynamic Data Handling:
array/object structures in API wrappers or config-as-code:
$config = WeakMap::from([
'database' => [
'default' => 'mysql',
'connections' => ['mysql' => ['host' => 'localhost']],
],
]);
config/app.php or service providers for hot-reloadable configs.Memory-Sensitive Operations:
WeakMap to avoid memory leaks:
$cache = new WeakMap();
$cache->set('large_object', $expensiveToLoadObject);
Event::listen(function () use (&$weakSet) {
$weakSet->add($this); // Listener will be GC’d after event
});
Hybrid Type Systems:
class PluginConfig {
public WeakMap $settings;
public string $name;
public function __construct() {
$this->settings = WeakMap::from([]);
}
}
Laravel-Specific Workflows:
$app->singleton(WeakMap::class, function () {
return new WeakMap(['default' => 'value']);
});
use Illuminate\Http\Request;
use Boson\WeakTypes\WeakType;
$request->merge(WeakType::from($request->all()));
Avoid Breaking Strong Types:
WeakCacheStore class to wrap Illuminate\Cache\Store.Leverage Laravel’s Events:
event(new UserProcessed($user, WeakMap::from(['metadata' => $data])));
Testing:
$weakMap = Mockery::mock(WeakMap::class);
$weakMap->shouldReceive('get')->andReturn('mocked_value');
Performance Tuning:
$weakMap = WeakMap::from(range(1, 1000000));
$strongArray = range(1, 1000000);
// Compare memory usage with `memory_get_usage()`.
Unexpected Object Collection:
$weakMap = new WeakMap();
$obj = new stdClass();
$weakMap->set('key', $obj);
unset($obj); // $weakMap->get('key') may return null!
Circular References:
$a = new stdClass(); $a->b = $b;
$b = new stdClass(); $b->a = $a;
$weakMap->set('a', $a); // Both objects may linger.
WeakSet for unique weak references or manual cleanup.Laravel Cache Driver Issues:
file, database) ignore weak references during serialization:
Cache::put('weak_key', $weakMap); // May store a serialized copy!
Redis or Memcached) that support weak references.Type Confusion:
$weakMap->set('age', '25'); // Stored as string
$age = $weakMap->get('age') + 1; // TypeError!
WeakType for stricter contracts.Debugging Challenges:
var_dump() or dd() may not show weak references as expected:
dd($weakMap->get('key')); // May output `null` even if the object exists.
WeakType::inspect() for debugging or log strong references.Check for Strong References:
$obj = new stdClass();
$weakMap->set('obj', $obj);
var_dump($weakMap->get('obj')); // null if no strong references
var_dump($obj); // Still exists if referenced elsewhere
Force Collection:
$weakMap->clear(); // Manually trigger GC
gc_collect_cycles(); // Force garbage collection (PHP 8.1+)
Log Weak Structure State:
$weakMap->onCollection(function () {
Log::debug('WeakMap collected keys: ' . implode(',', $this->keys()));
});
PHP 8.4+ Features:
composer.json enforces:
"config": {
"platform": {
"php": "8.4.0"
}
}
Laravel Service Provider Conflicts:
boson-php packages, alias classes explicitly:
use Boson\WeakTypes\WeakMap as BosonWeakMap;
Environment-Specific Behavior:
Custom Weak Structures:
WeakType to create domain-specific weak types:
class WeakUser extends WeakType {
public function getName(): string {
return $this->get('name');
}
}
Laravel Service Providers:
$app->when(WeakMap::class)
->needs('$config')
->give(fn () => config('weak_types.defaults'));
Event Listeners for Weak Structures:
$weakMap->onCollection(function (array $collectedKeys) {
// Trigger a backup or fallback
});
Integration with Laravel Collections:
Illuminate\Support\Collection to weak structures:
$weakMap = WeakMap::from(collect($array)->toArray());
How can I help you explore Laravel packages today?