Jump to content

popen and include files


bestsurfer

Recommended Posts

I have a php file which is executed with popen.

 

I am using an include in that file to get config variables. The config file included is a class.

 

class config

{

    function config()

    {

      // Database related configurations

        $this->db_engine      = 'mysql';

        $this->db_username  = 'root';

        $this->db_password  = '';

  }

}

 

 

If I include the file and try to retrive the config values using $cfg=new config(); Apache hangs and the CPU usage shoots to 100%.

 

If I remove the config file and use something like :

 

$cfg->db_engine      = 'mysql';

$cfg->db_username  = 'root';

$cfg->db_password  = '';

 

Everything is fine.

 

 

Could you please suggest whats wrong here?

 

 

The popen call is  $proc = popen('php ' . $path . $file . ' ' . $args . ' &', 'r');

 

Thanks

 

Ashish

Link to comment
https://forums.phpfreaks.com/topic/165165-popen-and-include-files/
Share on other sites

Hi Ashish,

 

Firstly, if you are including the file (as you would with class files), you want to be using either include, include_once, require, or require_once (depending on your needs regarding file references). For class files, I always use require_once.

 

Also, you are forming a class constructor the PHP4 OO way. Assuming the server you are running your PHP on has PHP5 installed, you want to use __construct as oppose to naming a method the same name as the class to run a constructor function.

 

so, in your view file, you want to put:

 

<?php
require_once('path-to-file.php');
?>

 

In the class, you want to use:

 

<?php
class config
{

    public $db_engine = 'mysql';
    public $db_username = 'root';
    public $db_password = '';

    function __construct()
    {
      // Database related configurations
        $this->db_engine      = 'mysql';
        $this->db_username  = 'root';
        $this->db_password  = '';
   }
}
?>

 

Cheers

Ironhamster88

 

Don't use a pointer i.e popen()

Fork the script with exec()

 

exec("php /path/to/script.php > /dev/null &");

 

Also, you are forming a class constructor the PHP4 OO way. Assuming the server you are running your PHP on has PHP5 installed, you want to use __construct as oppose to naming a method the same name as the class to run a constructor function.

ironhamster88, these comments have nothing to do with a performance issue!

@ironmaster

Tried declaring all variables, used __construct() still nothing.

error_log("conf start config.php".date('h:i:s'));
require_once("/var/www/codebase/web/config.php");
$cfg=new conf();
error_log("conf done config.php".date('h:i:s'));

Never reaches the 4th line. All errors and notices are on, and there are no notices or errors.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.