1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved Custom config not saving

Discussion in 'Development' started by mlsdmitry0, Dec 2, 2019.

  1. mlsdmitry0

    mlsdmitry0 Spider

    Messages:
    12
    --- SOLVE ---
    Code:
    parent::__construct($path, Config::JSON);
    foreach ($this->getAll(true) as $key) {
        if (property_exists($this, $key)) {
            $this->$key = $this->get($key);
        }
    }
    property_exists instead isset($this->$key)
    --- END SOLVE ---
    I have made class ArenaData extending Config. After editing I save it, it works.. After restart I recreate it and it resets to zero.
    Code
    Code:
    $config = 'world.json' (exam)
    world.json were { "level": "world"}
    $ad = new ArenaData(BuildBattle::getInstance()->getDataFolder() . $config);
    after new ArenaData {"level": null}
    I know why it happened, how to fix?:/
    --- need to fix ---
    array_filter((array)$this, function ($k) : bool {
                return strpos($k, Config::class) === false;
            }, ARRAY_FILTER_USE_KEY)
    --- end need to fix ---
     
    class ArenaData extends Config
    {
        public $level;
        public function __construct (string $path)
        {
            parent::__construct($path, Config::JSON, array_filter((array)$this, function ($k) : bool {
                return strpos($k, Config::class) === false;
            }, ARRAY_FILTER_USE_KEY));
            foreach ($this->getAll(true) as $key) {
                if (isset($this->$key)) $this->$key = $this->get($key);
            }
        }
    
        public function __set ($k, $v)
        {
            $this->$k = $v;
            $this->set($k, $v);
        }
    
        public function __get ($k)
        {
            return $this->get($k);
        }
    
        public function save () : bool
        {
            $this->setAll(
                array_filter((array)$this, function ($k) : bool {
                    return strpos($k, Config::class) === false;
                }, ARRAY_FILTER_USE_KEY)
            );
            return parent::save();
        }
    }
     
    Last edited: Dec 2, 2019
  2. NTT

    NTT Zombie

    Messages:
    311
    GitHub:
    NTT1906
    Have you try
    $test->save();?
     
  3. mlsdmitry0

    mlsdmitry0 Spider

    Messages:
    12
    Yeah:) of course, I write about it upper.
     
  4. mlsdmitry0

    mlsdmitry0 Spider

    Messages:
    12
    And also I remove lines marked as ---need to fix---. It started working, but $ad->level not working. Only this: $ad->get("level")
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.