bestsurfer Posted July 8, 2009 Share Posted July 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165165-popen-and-include-files/ Share on other sites More sharing options...
bestsurfer Posted July 8, 2009 Author Share Posted July 8, 2009 One thing I forgot to mention, the script runs well on the command prompt, only hangs when I use popen on Apache/PHP Quote Link to comment https://forums.phpfreaks.com/topic/165165-popen-and-include-files/#findComment-870883 Share on other sites More sharing options...
ironhamster88 Posted July 8, 2009 Share Posted July 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165165-popen-and-include-files/#findComment-870886 Share on other sites More sharing options...
JonnoTheDev Posted July 8, 2009 Share Posted July 8, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/165165-popen-and-include-files/#findComment-870888 Share on other sites More sharing options...
bestsurfer Posted July 8, 2009 Author Share Posted July 8, 2009 @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. Quote Link to comment https://forums.phpfreaks.com/topic/165165-popen-and-include-files/#findComment-870899 Share on other sites More sharing options...
bestsurfer Posted July 8, 2009 Author Share Posted July 8, 2009 @neil.johnson Thanks works with exec(), still wondering what might be the problem with popen(); I might need popen in the future, but for now will go with exec(); Thanks Ashish Quote Link to comment https://forums.phpfreaks.com/topic/165165-popen-and-include-files/#findComment-870909 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.