开发者

How to avoid writing include_once() in file again and again for including different class?

I have code structure in which I have class file, template file and controller file. And in every conroller file I have to call atleast 6 include_once(); How to avoid writing include_once() in file again and again for including differen开发者_运维技巧t class?


Take a look at PHP 5's autoloading mechanism.

It takes care of including the necessary files when the class is used.

In addition to saving you tons of include statements, only includes actually used in the current context will be loaded.


include_once is handy, no doubt,
but handy always pay a price
check discussion about include_once/require_once

I suggest you Should check before doing any include/include_once,
and wrap it as function

such as

function include_more($classes)
{
  foreach ($classes as $class)
  {
    if (!class_exists($class)) 
    {
      include $class.'.class';
    }
  }
}

In your controller, implement

include_more( array('Database', 'View', ...);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜