有没有办法将变量从扩展框架类放入控制器中?

发布于 2024-10-13 20:32:20 字数 885 浏览 0 评论 0原文

你好,我仍在通过制作一个来学习 MVC,今天我意识到我错过了事情是如何工作的。

class Framework
{

    function __construct()
    {
        require 'libraries/language/l.php';
        /*
        $l['hello'] = 'hello';
        $l['helloworld'] = 'helloworld';
        etc
        */
    }
}

class Controller extends Framework
{

    function index()
    {
        #missing ?
        echo $l;
    }
}

好的,第一个问题是我如何从我的控制器文件中 echo $l ?有办法做到这一点吗?

编辑*对此相同。

function library( $lib ){

    if (file_exists('libraries/lib.'. $lib .'.php')) {
        require 'libraries/lib.'. $lib .'.php';

        if (class_exists($lib)) {
            $class = ucfirst($lib);
            $$lib = new $class;
            return TRUE;
        }

        if (!class_exists($lib)) {
            return FALSE;
        }
    }
}

感谢您的关注。

Adam ramadhan

hello im still on learning mvc by makeing one, and today i realize that i have a miss on how things work.

class Framework
{

    function __construct()
    {
        require 'libraries/language/l.php';
        /*
        $l['hello'] = 'hello';
        $l['helloworld'] = 'helloworld';
        etc
        */
    }
}

class Controller extends Framework
{

    function index()
    {
        #missing ?
        echo $l;
    }
}

ok the first question is how can i echo $l from my controller files ? is there a way to do that ?

edit* same for this.

function library( $lib ){

    if (file_exists('libraries/lib.'. $lib .'.php')) {
        require 'libraries/lib.'. $lib .'.php';

        if (class_exists($lib)) {
            $class = ucfirst($lib);
            $lib = new $class;
            return TRUE;
        }

        if (!class_exists($lib)) {
            return FALSE;
        }
    }
}

thanks for looking in.

Adam ramadhan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声情话 2024-10-20 20:32:35

嗯,这意味着对于每个控制器实例,您将在其中保留一个大数组。

,您可以创建一个提供文本翻译的单例类

class Language
{
    private static $instance;
    public $l = array();

    private function __construct() {
        require 'libraries/language/l.php';
        $this->l = $l;
    }

    public static function getInstance() {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }
}

实际上 你可以有一个简写函数:

function l($text) {
    return Language::getInstance()->l[$text];
}

然后使用它:

echo l('hello') . "\n";

Well, that means for each Controller instance, you are going to keep a big array inside it.

Actually, you can make a singleton class that provides translation for text:

class Language
{
    private static $instance;
    public $l = array();

    private function __construct() {
        require 'libraries/language/l.php';
        $this->l = $l;
    }

    public static function getInstance() {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }
}

And you can have a shorthand function for it:

function l($text) {
    return Language::getInstance()->l[$text];
}

And then use it:

echo l('hello') . "\n";
月隐月明月朦胧 2024-10-20 20:32:30

通过对象受保护的属性传递数据:

class Framework
{
    protected $l = array();

    function __construct()
    {
        require 'libraries/language/l.php';

        $this->l['hello'] = 'hello';
        $this->l['helloworld'] = 'helloworld';
    }
}

class Controller extends Framework
{

    function index()
    {
        echo $this->l['hello'];
    }
}

Pass the data through object protected properties:

class Framework
{
    protected $l = array();

    function __construct()
    {
        require 'libraries/language/l.php';

        $this->l['hello'] = 'hello';
        $this->l['helloworld'] = 'helloworld';
    }
}

class Controller extends Framework
{

    function index()
    {
        echo $this->l['hello'];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文