简体   繁体   中英

php cli popen() include file once?

I am doing this in my main file lets say index.php

$handle = popen("php -q nah.php -p=". escapeshellarg($part) . " 2>&1", "r");
while (!feof($handle))
{
    $read = fread($handle, 2096);
    echo $read;
}
pclose($handle);

What i want is that i dont include a file containing a number of classes in the nah.php and not including it in the nah.php because the nah.php gets provoked using a while loop so if i include in it in nah.php it will slow down a bit.

Is it possible?

If you are saying that, while PHP is in CLI mode, you don't want to include files that would otherwise be included by nah.php , do:

if (php_sapi_name() !== 'cli')
{
  // include files here
}

Otherwise... just don't include the files... ?

Or go for pcntl_fork for this one (if this is in CLI, don't do it in a webserver for a variety of reasons). You'll only have to load all class/function definitions once (but don't open resources yet, for instance, a database connection should be opened in the child). Getting the data back is then a little bit more work, but not terribly difficult. I don't think you'll get any significant speed gain however...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM