Search code examples
phpclassooptraits

php trait using another trait


I have a trait that is using another trait, and now I'm getting errors about functions that don't exist in classes.

I have simplified the code:

settings.php

<?php

trait settings {
    // read setting from config.ini
    protected function getSetting($type, $setting){
        try {
            $configFile = dirname(__FILE__)."/../config.ini";
            if (!file_exists($configFile) || !is_file($configFile))
                throw new Exception("Config file was not found. ");

            $configContents = parse_ini_file($configFile,true);
            if (is_array($configContents) && array_key_exists($type, $configContents) && is_array($configContents[$type]) && array_key_exists($setting, $configContents[$type]))
                return $configContents[$type][$setting];
            else
                throw new Exception("Setting ".$setting." could not be found in ".$type.".");
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }
}

database.php

<?php
trait database {
    use settings, session;

    private $pdo;

    // connect to database
    protected function connect() {
        try {
            $this->pdo = new PDO(
                "mysql:host=".$this->getSetting("db", "host").";dbname=".$this->getSetting("db", "database"),
                $this->getSetting("db", "user"),
                $this->getSetting("db","password")
            );

            $this->init();
        } catch (PDOException $e) {
            throw new Exception($e->getMessage());
        }
    }
}

users.php

<?php

class users {
    use database;

    public function __construct() {
        try {
            $this->connect();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function __destruct() {
        unset($this);
    }

    public function isAdmin() {
        try {
            if ($this->loginStatus() === true) {
            } else
                return false;
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function loginStatus() {
        if (!$this->getSession("tysus") || !$this->getSession("tyspw"))
            // user is not logged in because we couldn't find session with username and/or password
            return false;

        if (!$this->userExists($this->getSession("tysus"), $this->getSession("tyspw")))
            // user is unknown to database
            return false;

        // other checks failed, user must be logged in
        return true;
    }
}

And now I'm getting this error:

Fatal error: Call to undefined method users::readSetting() in /home/deb2371/domains/nonamenohistory.com/public_html/include/classes/class.database.php on line 18

What I thought would happen was something like this: Class users uses database trait and that trait would use settings and session traits.

If that were the case, I wouldn't be getting any errors, but unfortunately, this isn't the case.

Does someone know how to fix this problem?


Solution

  • Your issue may be due to you using readSetting but it is actually called getSetting