简体   繁体   English

从其他类函数调用静态函数不起作用

[英]Calling static functions from other class functions isnt working

I am trying to achieve the goal of static configuration data, where I can grab config items from an array at any file I want. 我试图实现静态配置数据的目标,在这里我可以从阵列中的任意文件中获取配置项。 I have made the functions static and it still is saying class could not be found? 我已经将函数设为静态,但仍然说找不到类? I am pretty new to PHP so help me out... 我对PHP还是很陌生,所以请帮帮我...

Index.php: Index.php:

include(ROOT . '/application/library/yoda/settings.class.php');

if (class_exists('Settings')) {
    Settings::setPublic('testpublic', 'This is a test setting for the public category.');
}

Settings class: 设置类别:

class Settings
{
    static private $protectedSettings = array(); // For DB / passwords etc
    static private $publicSettings = array(); // For all public strings such as meta stuff for site

    /*public function __construct() {
        $this->protectedSettings = array();
        $this->publicSettings = array();
    }*/

    public static function getProtected($key) {
        return isset(self::$protectedSettings[$key]) ? self::$protectedSettings[$key] : false;
    }

    public static function getPublic($key) {
        return isset(self::$publicSettings[$key]) ? self::$publicSettings[$key] : false;
    }

    public static function setProtected($key,$value) {
        self::$protectedSettings[$key] = $value;
    }

    public static function setPublic($key,$value) {
        self::$publicSettings[$key] = $value;
    }
}

Where the error displays is in the code below... when I call showConfig() 错误显示在下面的代码中...当我调用showConfig()时

class template {

    public function showConfig() {
        exit(Settings::getPublic('testpublic'));
    }
}

When calling showConfig() it displays the error below... 当调用showConfig()时,它显示以下错误...

Fatal error: Class 'Settings' not found 致命错误:找不到类“设置”

Make sure you are including the Settings class file in the file that has the template class. 确保在包含模板类的文件中包括设置类文件。

Other than that the code looks good and should be working. 除此之外,代码看起来不错,应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM