/
factory.php
47 lines (45 loc) · 1.12 KB
/
factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
abstract class ImageType{
protected $type;
function returnType(){
return $this->type;
}
}
class GIFType extends ImageType{
function __construct($fileName){
$this->type = 'gif';
}
}
class JPGType extends ImageType{
function __construct($fileName){
$this->type = 'jpg';
}
}
class PNGType extends ImageType{
function __construct($fileName){
$this->type = 'png';
}
}
class OtherType extends ImageType{
function __construct($fileName){
$this->type = 'неизвестен';
}
}
class ImageFactory{
static function checkType($fileName){
if(strpos($fileName, '.gif'))
return new GIFType($fileName);
elseif(strpos($fileName, '.jpg'))
return new JPGType($fileName);
elseif(strpos($fileName, '.png'))
return new PNGType($fileName);
else
return new OtherType($fileName);
}
}
$fileNames = array('test.jpg','test.txt','test.gif','test.png');
foreach($fileNames as $fileName){
print "Тип файла ".$fileName." ";
print ImageFactory::checkType($fileName)->returnType().".<br>";
}
?>