包括何时调用函数

时间:2013-01-26 18:29:30

标签: php

我想只在调用方法时才包含文件。 所以,如果我正在做这样的事情:

class foo
{
   function printer()
   {
      //Do something
      return $something;
   }

   function some_math($a, $b)
   {
      if($a == $b)
      {
          require_once('path/to/some_class.php');
          $instance = new some_class();
          //Do something with some_class
      }
      else
      {
          //Do another things
      }
      return $some_result;
   }

}

$var = new foo();
$var->some_math(2, 3);

在这种情况下是否会解析some_class.php?

我有一些繁重的库,我不想在他们不需要时解析它们。

这是正常的解决方案吗?如果没有,我怎么解决这个问题? =)(Autoload没有按照我的意愿工作。当我使用它时,autoload每次都包含文件。)

1 个答案:

答案 0 :(得分:0)

只有在调用$ var-> some_math()AND $ a == $ b时才会加载some_class.php。但是,像其他人一样提到过;它很容易测试;

将其添加到some_class.php的开头;

echo 'Hello world'; exit();

并运行您的代码而不调用$ var-> some_math(..);不应出现“Hello world”。

然后修改您的代码并执行调用$ var-> some_math(1,1);应该出现“Hello world”

相关问题