Supported types
Finished the video? Learn more about using PHP code for injection >
There are 4 kinds of supported types:
- Primitive types
- Classes and interfaces
- Arrays (and associative arrays)
- Mixed types
Note: if you are using a third party library that requires injecting a type that is not supported, you can always fallback to injecting values via PHP code.
Primitive types
Mouf supports those primitive types: string
,
char
,
bool
,
boolean
,
int
,
integer
,
double
,
float
,
real
,
mixed
,
number
If your type is one of those, you will be able to input text directly in the property in Mouf UI. Note that there is a special behaviour for "bool" and "boolean". They are rendered as a checkbox in Mouf UI.
For instance:
class Wizard {
...
/**
* @param string $name The name of the wizard
* @param int $hp The hit points of the wizard
* @param bool $isAlive Whether the wizard is alive or not
*/
public function __construct($name, $hp, $isAlive) {
...
}
...
}
Classes and interfaces
We already saw that classes and interfaces can be used as types in the example above.
In the Mouf user interface, these properties will be displayed like this:
class Wizard {
...
/**
* The best pal of the wizard.
* @param Warrior $sideKick
*/
public function setSideKick(Warrior $sideKick) {
$this->sideKick = $sideKick;
}
...
}
Arrays
Mouf does a difference between indexed arrays and associative arrays (aka maps). Inside an array, you can inject primitive types or objects (but you cannot mix both, unless you use the injection via PHP code feature).
Indexed arrays
Here are 2 samples of indexed arrays.
class Wizard {
...
/**
* The words the wizard pronounces.
* @param array<string> $wordsOfPower
*/
public function setWordsOfPower(array $wordsOfPower) {
$this->wordsOfPower = $wordsOfPower;
}
/**
* The spells this wizard knows.
* @param SpellInterface[] $spells
*/
public function setSpells(array $spells) {
$this->spells = $spells;
}
...
}
As you can notice, there are 2 ways to write arrays: using brackets or the array keyword.
Therefore, string[]
and array<string>
are similar.
Associative arrays (aka maps)
Here are 2 samples of maps.
class Wizard {
...
/**
* This wizard's masters => a map of Wizard objects
* @param array<string, Wizard> $masters
*/
public function setMasters(array $masters) {
$this->masters = $masters;
}
/**
* This wizard's name in many contries.
* The key is the country code, the value is the name.
* @param array<string, string> $internationalNames
*/
public function setInternationalNames(array $internationalNames) {
$this->internationalNames = $internationalNames;
}
...
}
As you can notice, associative arrays (or maps) can only be achieved using the array<xxx, xxx> notation.
Generic "array" type
The "pure" array
type is unsupported.
class Wizard {
...
/**
* This is not supported
* @var array
*/
public $unsupported;
}
array
. Mouf has no clue what can be injected in an
array (is it an array of strings? of objects? a map? In this case, you can still inject the value
you want by using the inject property by PHP code
option that will let you type some PHP code to fill the property.Mixed types
Mouf does not support the mixed keyword. However, you can use a pipe to declare that a property can have many different types.
For instance:
string|Healer|Warrior|Wizard
is a valid type
Let's rewrite the "sideKick" setter and assume that the sidekick of our wizard can be anything (a warrior, a healer, another wizard or simply a string containing the name of someone:
class Wizard {
...
/**
* The best pal of the wizard.
* @param string|Healer|Warrior|Wizard $sideKick
*/
public function setSideKick($sideKick) {
$this->sideKick = $sideKick;
}
...
}
Using the UI, you can choose the type you want to inject by clicking on it.
Found a typo? Something is wrong in this documentation? Just fork and edit it!