is there a way puting variables in controller from an extended framework class?
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 ca开发者_StackOverflow中文版n 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
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'];
}
}
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";
精彩评论