FuelPHP 擴充實作篇

Posted by ayuayu on 2014/07/24

學官網在classes下面直接加入 class MyConfig extends Config,然後隨便呼叫一個已經設定的設定值

echo "MyConfig::get('config_key1') = ".MyConfig::get('config_key1');

得到MyConfig::get('config_key1') = config_value1

隨便裡面加個函數並echo "MyConfig::test() = ".MyConfig::test();
public static function test(){
    return 7;
}
得到MyConfig::test() = 7

因此隨便副寫個get
public static function get($item, $default = NULL){
    return 1;
}

結果MyConfig::get('config_key1')輸出1,代表可以簡單用自訂函數去override

但是如果一定要使用官方的核心指令,那就必須要用同名class 同名函數去override
另外也必須正確把這個同名class在bootstrap當中預先add class,才能生效

實際上,FuelPHP有擴充限制,具體限制那些在官網可以看到
http://fueldocs.ycnets.com/general/extending_core.html

example 複寫log
class Log extends Fuel\Core\log{
 public static function info($msg, $method = null){
  return static::write(\Fuel::L_INFO, 'troll my info method ver2'.$msg, $method);
 }
}

Autoloader::add_classes(array(
    'Log'=>APPPATH.'classes/log.php',
));

如此,以後紀錄的info訊息都會加上一些自訂文字在裡面

沒有留言:

張貼留言