Transform class with static methods in interface


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::$list[] = $item;

    public static function remove($item) {
        for ($i = 0; $i < self::size(); $i++) {
            if (self::get($i) === $item) {
        self::$list = array_values(self::$list);

    public static function get($indice) {
        return self::$list[$indice];

    public static function size() {
        return sizeof(self::$list);

    private static function gravar() {
        $texto = serialize(self::$list);
        $a = fopen(dirname(__DIR__) . '/model/DB.txt', 'w');
        fwrite($a, $texto);

    public static function atualizarDB() {


What I'm trying to do would be something like this:

interface ArrayList{
asked by anonymous 25.11.2015 / 14:11

1 answer


The first thing you would have to do is to stop using static elements in this class. It does not make sense to use an interface in an essentially static class.

I find it strange to have a database update method inside a generic data structure like this. I think this should be out, but it's up to you.

I also do not know the requirements and exactly the purpose of creating the interface, but I believe what you want is this:

interface iArrayList {
    public function add($item);
    public function remove($item);
    public function get($indice);
    public function size();

If you want you can put other methods that are mandatory and general.

Then this class, besides the suggested changes would be written like this:

class ArrayListDB implements iArrayList

The next one would probably be:

class ArrayListJson implements iArrayList
25.11.2015 / 14:21