1. Go to this page and download the library: Download skollro/factory library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
skollro / factory example snippets
use function Skollro\Factory\make;
class VehicleFactory
{
public static function make($type)
{
return make($type)
->resolve('car', Car::class)
->resolve('bike', Bike::class)
->resolve([
'truck' => Truck::class,
'bus' => Bus::class,
])
->otherwiseThrow(InvalidArgumentException::class);
}
}
$car = VehicleFactory::make('car'); // returns instance of Car
$truck = VehicleFactory::make('truck'); // returns instance of Truck
$motorcycle = VehicleFactory::make('motorcycle') // throws InvalidArgumentException
use Skollro\Factory\Factory;
use function Skollro\Factory\make;
$factory = make($type);
$factory = Factory::make($type);
$object = make('bike')
->resolve('car', Car::class)
->resolve('bike', function () {
return new Bike;
})
->resolve('bike', MountainBike::class) // this is not the first match and thus not the result
->resolve([
'truck' => Truck::class,
'bus' => Bus::class,
])
->otherwiseThrow(InvalidArgumentException::class);
// $object is an instance of Bike
$object = make('motorcycle')
->resolve([
'car' => Car::class,
'bike' => Bike::class,
])
->otherwise(Vehicle::class);
// $object is an instance of Vehicle
$object = make('motorcycle')
->resolve([
'car' => Car::class,
'bike' => Bike::class,
])
->otherwise(function () {
return new Vehicle;
});
// $object is an instance of Vehicle
// in your AppServiceProvider
Factory::resolveUsing(function ($className) {
return app($className);
});
// in your code
$object = make('car')
->resolve('car', Car::class)
->otherwiseThrow(InvalidArgumentException::class);
// $object is an instance of Car, resolved by Laravel container
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.