Jump to content

Recommended Posts

Hi everyone. Please help me please. I'm just learning PHP. There was a task, I completed it, but they sent a response that it needs to be modified. You need to make changes: "When saving data, you need to take into account the keys of the new data and the data stored in the database(file), if the value with the key in the database(file) already exists, then you need to replace its value with a new one." and "The FileBox and DbBox classes should be implemented in such a way that you can not create more than one instance of each of the classes."

Googled and came across what i need to apply the singleton pattern.

<?php

interface Box
{
    public function setData($key, $value);

    public function getData($key);

    public function save();

    public function load();
}

abstract class BoxAbstract implements Box
{
    protected $data = [];

    public function setData($key, $value)
    {
        $this->data[$key] = $value;
    }

    public function getData($key)
    {
        return $this->data[$key] ?? null;
    }

    public abstract function save();

    public abstract function load();
}

class FileBox extends BoxAbstract
{
    private $file;

    public function __construct($file)
    {
        $this->file = $file;
    }

    public function save()
    {
        file_put_contents($this->file, serialize($this->data));
    }

    public function load()
    {
        $this->data = unserialize(file_get_contents($this->file));
    }
}

class DbBox extends BoxAbstract
{
    private $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function save()
    {
        $this->pdo->beginTransaction();

        $this->pdo->query('DELETE FROM box')->execute();

        $stmt = $this->pdo->prepare("INSERT INTO box (key, value) VALUES (:key, :value)");

        foreach ($this->data as $key => $value) {
            $stmt->bindValue(':key', $key);
            $stmt->bindValue(':value', serialize($value));
            $stmt->execute();
        }
        $this->pdo->commit();
    }

    public function load()
    {
        $stmt = $this->pdo->query('SELECT key, value FROM box_items');

        $data = [];

        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            $data[$row['key']] = unserialize($row['value']);
        }

        $this->data = $data;
    }
}

 

Link to comment
https://forums.phpfreaks.com/topic/312170-i-need-to-make-some-changes-to-the-code/
Share on other sites

Define your primate keys as 'UNIQUE' in your schema. When you try to add a duplicate key, you will get an error. Handle that error by adding the new key.

As for the class question, you are being asked to create a singleton class.

1 hour ago, gw1500se said:

Define your primate keys as 'UNIQUE' in your schema.

Primary keys are unique identifiers for each entity and therefore, by definition, are already UNIQUE. (Or did you really mean monkeys? 🙂 )

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.