Features - maartenba/phpmef GitHub Wiki
This page will highlight the features of PHPMEF.
PHPMEF will always try to match one or more exports with a defined import. The most simple example matches both in a 1:1 manner, using the @import
annotation:
class SomeClass {
/**
* @import IKnowNothing
*/
public $instance;
}
/**
* @export IKnowNothing
*/
class SomeOtherClass implements IKnowNothing {
// ...
}
$someClassInstance = new SomeClass();
$compositionInitializer = new MEF_CompositionInitializer(new MEF_Container_Default());
$compositionInitializer->satisfyImports($someClassInstance );
The $someClassInstance->instance
property will contain an instance of SomeOtherClass
after composition.
PHPMEF will always try to match one or more exports with a defined import. The most simple example matches both in a 1:N manner, using the @import-many
keyword:
class SomeClass {
/**
* @import-many IKnowNothing
*/
public $instances;
}
/**
* @export IKnowNothing
*/
class SomeOtherClassA implements IKnowNothing {
// ...
}
/**
* @export IKnowNothing
*/
class SomeOtherClassB implements IKnowNothing {
// ...
}
$someClassInstance = new SomeClass();
$compositionInitializer = new MEF_CompositionInitializer(new MEF_Container_Default());
$compositionInitializer->satisfyImports($someClassInstance );
The $someClassInstance->instances
property will contain an array of instances of SomeOtherClassA
and SomeOtherClassB
after composition.
PHPMEF can take note of metadata specified on import and export definitions. The following example will only import an instance of IKnowNothing
if the metadata "CanHasFeatureX" is present:
class SomeClass {
/**
* @import IKnowNothing
* @import-metadata CanHasFeatureX
*/
public $instance;
}
/**
* @export IKnowNothing
* @export-metadata CanHasFeatureX
*/
class SomeOtherClass implements IKnowNothing {
// ...
}
$someClassInstance = new SomeClass();
$compositionInitializer = new MEF_CompositionInitializer(new MEF_Container_Default());
$compositionInitializer->satisfyImports($someClassInstance );
The $someClassInstance->instance
property will contain an instance of SomeOtherClass
after composition. If the @export-metadata CanHasFeatureX
definition would not have been specified on SomeOtherClass
, the composition would not have succeeded because no export would have been found that had matching metadata.
Note: Metadata for a specific class can be retrieved by using $compositionInitializer->getMetadataForClass(...)
By default, PHPMEF instantiates a new object every time an import has to be satisfied. There are situations where it is desirable to have only one instance of a specific class available, using it over and over again, no matter where and how much it is being imported. Using the @export-metadata singleinstance
keyword, this can be achieved:
class SomeClass {
/**
* @import IKnowNothing
*/
public $instance;
}
/**
* @export IKnowNothing
* @export-metadata singleinstance
*/
class SomeOtherClass implements IKnowNothing {
// ...
}
$someClassInstance = new SomeClass();
$compositionInitializer = new MEF_CompositionInitializer(new MEF_Container_Default());
$compositionInitializer->satisfyImports($someClassInstance );
The $someClassInstance->instance
property will contain an instance of SomeOtherClass
after composition. There will only be one and exactly one instance of SomeOtherClass
in memory, even if multiple classes import this class.
Note: When using multiple MEF_CompositionInitializer
instances, one instance of SomeOtherClass
will be created per MEF_CompositionInitializer
instance.
PHPMEF allows you to export class members. This means that a property or function can be created, and that the return value of it will be used as the exported instance. The following example imports a series of strings from some classes that have a member export defined, Hello
and World
.
class HelloWorld {
/**
* @import-many MessageToSay
*/
public $Messages;
public function sayIt() {
echo implode(', ', $this->Messages);
}
}
class Hello {
/**
* @export MessageToSay
*/
public $HelloMessage = 'Hello';
}
class World {
/**
* @export MessageToSay
*/
public $WorldMessage = 'World!';
}
$helloWorld = new HelloWorld();
$compositionInitializer = new MEF_CompositionInitializer(new MEF_Container_Default());
$compositionInitializer->satisfyImports($helloWorld);
$helloWorld->sayIt(); // Hello, World!