Skip to content

Instantly share code, notes, and snippets.

@schlessera schlessera/Plugin.php
Last active Sep 29, 2017

Embed
What would you like to do?
Using Value Objects for Dependency Injection
<?php
$plugin = new Plugin( new PluginRoot( __FILE__ ) );
<?php
class Plugin
{
protected $plugin_root;
private $loaded;
public function __construct(PluginRoot $plugin_root)
{
$this->loaded = false;
$this->plugin_root = $plugin_root;
}
public function isLoaded()
{
return $this->loaded;
}
public function load()
{
// ...
}
public function getSubscribers()
{
return [
new OtherPluginClass($this->plugin_root)
];
}
}
<?php
class PluginRoot {
protected $file;
public function __construct( $file ) {
$this->file = $file;
}
public function get_path() {
return plugin_dir_path( $this->file );
}
public function get_url() {
return plugin_dir_url( $this->file );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.