I start with an example.
Mold class
<?php
class Mold {
public $current;
function merge($first, $second) {
$this->current = $first . $second;
return $this;
}
function phone() {
$this->current = '<a href="tel:' . $this->current . '">' . $this->current . '</a>';
return $this;
}
function __toString() {
return $this->current;
}
}
function mold() {
return new Mold();
}
In action
This works as expected.
echo mold()->merge('sada', 'asdsa')->phone();
Problem
I have one or more classes with methods that wants to be available as well.
Plugin class
class MyPlugin {
function link() {
// Code
}
function currency($number) {
// Code
}
}
class MyPlugin2 {
// Other methods
}
Dream code
The exact change of events below may not make any sense. Anyway, what I intend to do is the following.
echo mold()->merge('sada', 'asdsa')->currency(45)-link();
- Call
mold()
which creates a new instance of theMold
class. - Chain
merge()
which is used from theMold
class. -
currency()
orlink()
method does not exist in theMold
class. Instead they should be loaded from one of the plugins.
In conclusion
- I know I can extend a class but it does not really solve the problem because there can be more than one plugin classes.
- I know I can create instances of the plugin classes, but they somehow need to be aware by the
Mold
class. - Append methods to a class comes to mind as well as merge classes.