Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Last active August 27, 2021 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeschinkel/5b433532dc54adf53f5b239c8086fd63 to your computer and use it in GitHub Desktop.
Save mikeschinkel/5b433532dc54adf53f5b239c8086fd63 to your computer and use it in GitHub Desktop.
Idea: Uses PHP traits for a class from another file
<?php
// In /index.php
spl_autoload_register(fn($c) => require sprintf( "%s/src/%s.php", __DIR__, $c ));
// In /src/FooBarCms.php
class FooBarCms {}
// In /plugins/my-map/src/MyMap.php
class MyMap {
function display() {}
}
// In /plugins/my-map/src/MyMapper.php
trait MyMapper {
public $map;
function __construct() {
$this->map = new MyMap;
}
}
// In /plugins/my-map/index.php
spl_autoload_register(fn($c) => require sprintf( "%s/src/%s.php", __DIR__, $c ));
register_dynamic_trait( FooBarCMS::class, MyMapper::class, fn($instance) => $instance->map = new MyMap );
$cms = new FooBarCms();
$cms->map->display();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment