<?php
class File {
protected $resource;
protected function __construct() {
// objekt nepůjde zvenku přímo vytvořit
}
static function createFromFile($filename, $mode) {
$return = new self; // vytvoří objekt třídy File
// od PHP 5.3 lze místo self použít static - vytvoří i potomky
$return->resource = fOpen($filename, $mode);
return $return;
}
static function createFromSocket($hostname, $port = -1) {
$return = new self;
$return->resource = fSockOpen($hostname, $port);
return $return;
}
}
$file = File::createFromFile(__FILE__, "r");
<?php
// INTERFACES
interface WriterFactory
{
public function createCsvWriter(): CsvWriter;
public function createJsonWriter(): JsonWriter;
}
interface CsvWriter
{
public function write(array $line): string;
}
interface JsonWriter
{
public function write(array $data, bool $formatted): string;
}
// WRITTERS
class UnixCsvWriter implements CsvWriter
{
public function write(array $line): string
{
return join(',', $line) . "\n";
}
}
class WinCsvWriter implements CsvWriter
{
public function write(array $line): string
{
return join(',', $line) . "\r\n";
}
}
class UnixJsonWriter implements JsonWriter
{
public function write(array $data, bool $formatted): string
{
$options = 0;
if ($formatted) {
$options = JSON_PRETTY_PRINT;
}
return json_encode($data, $options);
}
}
class WinJsonWriter implements JsonWriter
{
public function write(array $data, bool $formatted): string
{
return json_encode($data, JSON_PRETTY_PRINT);
}
}
// FACTORIES
class UnixWriterFactory implements WriterFactory
{
public function createCsvWriter(): CsvWriter
{
return new UnixCsvWriter();
}
public function createJsonWriter(): JsonWriter
{
return new UnixJsonWriter();
}
}
class WinWriterFactory implements WriterFactory
{
public function createCsvWriter(): CsvWriter
{
return new WinCsvWriter();
}
public function createJsonWriter(): JsonWriter
{
return new WinJsonWriter();
}
}
// USAGE
$writeFactory = new WinWriterFactory();
$writerFactory->createJsonWriter(); // or $writerFactory->createCsvWriter()