I have a custom class called ArrayList
, which I created to handle objects in a small project of mine, but I would like to abstract it further by transforming it into an interface. This way, I could create other classes that do the same thing with different banks (in this class, saved in txt
, but I have another one that does the same things in a JSON file and in the future I'll make one to save in MySQL) .
How can I make this transformation, if it is feasible?
Follow the class ArrayList
class ArrayList {
private static $list = null;
private function __construct() {
}
private static function getList() {
if (!isset(self::$list)) {
$linha = "";
if (file_exists(dirname(__DIR__) . '/model/DB.txt')) {
$banco = dirname(__DIR__) . '/model/DB.txt';
$a = fopen($banco, 'r');
$linha = fread($a, filesize($banco));
self::$list = unserialize($linha);
} else {
self::$list = Array();
}
}
}
public static function add($item) {
self::getList();
self::$list[] = $item;
}
public static function remove($item) {
self::getList();
for ($i = 0; $i < self::size(); $i++) {
if (self::get($i) === $item) {
unset(self::$list[$i]);
break;
}
}
self::$list = array_values(self::$list);
}
public static function get($indice) {
self::getList();
return self::$list[$indice];
}
public static function size() {
self::getList();
return sizeof(self::$list);
}
private static function gravar() {
$texto = serialize(self::$list);
$a = fopen(dirname(__DIR__) . '/model/DB.txt', 'w');
fwrite($a, $texto);
fclose($a);
}
public static function atualizarDB() {
self::gravar();
}
}
What I'm trying to do would be something like this:
interface ArrayList{
getList();
add($item);
save();
}