-
-
Save mikeschinkel/5b433532dc54adf53f5b239c8086fd63 to your computer and use it in GitHub Desktop.
Idea: Uses PHP traits for a class from another file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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