包含何时调用函数[关闭]

I want to include file only if method is called. So, if I'm doing something like this:

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);

Does some_class.php will be parsed in this case?

I have some heavy libraries and I don't want to PHP parse them, when they unneeded.

Is this a normal solution? If not, how I can solve this? =) (Autoload did not work, as I would like. When I use it, autoload includes file every time).

some_class.php will only be loaded if $var->some_math() is called AND $a == $b. However, like others have mentioned; it's easy to test;

Add this to the start of some_class.php;

echo 'Hello world'; exit();

and run your code without calling $var->some_math(..); "Hello world" should not appear.

Then modify your code and do make a call to $var->some_math(1,1); "Hello world" should appear