dev-master
9999999-devphpABLE facade abstractions library
MIT
The Requires
- php >=7.2.0
- able/prototypes dev-master
by hacpaka
facade phpable
Wallogit.com
2017 © Pedro Peláez
phpABLE facade abstractions library
The phpABLE abstractions library provides the facade pattern implementation., (*1)
There's a simple way to install able/facades into your project via Composer:, (*2)
composer require able/facades
Let's design a simple class to explain how facades work:, (*3)
class Recipient {
private $name = "Unknown";
public function __construct(string $name) {
$this->name = $name;
}
public function changeName(string $name): void {
$this->name = $name;
}
public function sayHello(): void {
echo sprintf("Hello %s!", $this->name);
}
}
So the point to use a facade is an ability to call recipients method statically, without creating any instances directly. In this case, the facade class takes a gateway role. The advantage we gain - more transparent and understandable code., (*4)
use \Able\Facades\AFacade;
use \Able\Facades\Structures\SInit;
class FacadeExample extends AFacade {
/**
* The recipient class.
*/
protected static $Recipient = Recipient::class;
/**
* The initialize method can be used to provide
* some necessary arguments to the recipient class constructor if needed.
*/
protected final static function initialize(): SInit {
return new SInit(["John"]);
}
}
FacadeExample::sayHello();
// Hello John!
It's also possible to provide the callback function as a second field of the structure. This function going to be executed directly after the creation and will obtain the created object as an argument., (*5)
use \Able\Facades\AFacade;
use \Able\Facades\Structures\SInit;
class FacadeExample extends AFacade {
protected static $Recipient = Recipient::class;
protected final static function initialize(): SInit {
return new SInit(["John"], function(Recipient $Object){
$Object->changeName("Barbara");
});
}
}
FacadeExample::sayHello();
// Hello Barbara!
By default, the only instance of the recipient class going to be created.
This behavior is similar to a singleton pattern and could be changed via $keepSingle protected property., (*6)
use \Able\Facades\AFacade;
class FacadeExample extends AFacade {
/**
* If this property set to false, the new instance of the recipient object
* going to be created before any method call.
*/
protected static $keepSingle = false;
}
This package is released under the MIT license., (*7)
phpABLE facade abstractions library
MIT
facade phpable