doufu1504 2008-12-12 15:11
浏览 37
已采纳

管理单身人士最好的方法是什么?

I am messing around with different PHP logging frameworks. I am currently trying PEAR::Log. I figured that I would use its singleton function to make sure there was only one instance of the class around.

I have a small daemon-like script I wanted to add logging to because it was probably the simplest script in the system to test. This script has several functions. I will probably want to log things inside the functions.

The question I have is how do I best manage this singleton?

To me calling this:

&Log::singleton($handler, $name, $ident, $conf, $maxLevel);

in every function doesn't seem ideal especially since I already specified all of the options in the initial call. Pear::Log serializes this info, but from what it looks like you still have to provide all of those variables to get the instance.

Another alternative is passing the instance into every function. Again, seems like it's less than ideal.

I suppose you could make the instance a 'global' as well.

What do you in this situation? Are there better solutions?

  • 写回答

4条回答 默认 最新

  • doushi6947 2008-12-12 15:52
    关注

    I don't know much about PEAR::Log, but why not create another singleton that wraps/simplifies logging.

    class Logger {
    
        private static $log;
    
        private function __construct() { }
    
        public static function init(Log $log) {
            self::$log = $log;
        }
    
        public static function get() {
            return self::$log;
        }
    
    }
    

    Once you initialize Logger with Log instance you can access it via Logger::get. Since dereference is possible in PHP you can then do

    Logger::get()->doSomething($foo, $bar);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统