Static class crashing apache :-\

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Static class crashing apache :-\

Post by Chris Corbyn »

I re-wrote a configuration script my client had which was using globals to use a static class. For some reason he says it's crashing his server to the point he needs to restart apache. Any clues?

Code: Select all

<?php

/**
 * Static app configuration to use anywhere
 * @package XXXX
 * @author XXXX
 */
class Config
{
	/**
	 * Various config value from INI file
	 * @var array config
	 */
	private static $directives = array();
	
	/**
	 * Load the INI file and parse it
	 * @param string path
	 */
	public static function load($file)
	{
		if (file_exists($file)) self::$directives = parse_ini_file($file);
	}
	/**
	 * Check if a given directive exists
	 * @param stirng key
	 * @return bool
	 */
	public static function has($key)
	{
		return array_key_exists($key, self::$directives);
	}
	/**
	 * Get the value for $key, return null if not set
	 * @param string key
	 * @return mixed
	 */
	public static function get($key)
	{
		if (self::has($key))
		{
			return self::$directives[$key];
		}
	}
	/**
	 * Set a new directive or override an existing one
	 * @param string key
	 * @param mixed value
	 */
	public static function set($key, $value)
	{
		self::$directives[$key] = $value;
	}
	/**
	 * Remove a config directive if set
	 * @param string key
	 */
	public static function delete($key)
	{
		if (self::has($key))
		{
			self::$directives[$key] = null; //Saves memory since unset() doesn't free this up
			unset(self::$directives[$key]);
		}
	}
	/**
	 * Clear out all config directives
	 */
	public static function flush()
	{
		self::$directives = array();
	}
	/**
	 * Write the configuration as-is into a file
	 * @param string path
	 * @return bool
	 */
	public static function save($path)
	{
		$content = '';
		$sections = '';
		
		foreach (self::$directives as $key => $item)
		{
			if (is_array($item))
			{
				$sections .= "\n[{$key}]\n";
				foreach ($item as $key2 => $item2)
				{
					if (is_numeric($item2) || is_bool($item2)) $sections .= "{$key2} = {$item2}\n";
					else $sections .= "{$key2} = \"{$item2}\"\n";
				}
			}
			else
			{
				if(is_numeric($item) || is_bool($item)) $content .= "{$key} = {$item}\n";
				else $content .= "{$key} = \"{$item}\"\n";
			}
		}
		
		$content .= $sections;
		
		if (!$handle = fopen($path, 'w+')) return false;
		
		if (!fwrite($handle, $content)) return false;
		
		fclose($handle);
		return true;
	}
}
User avatar
hanji
Forum Commoner
Posts: 46
Joined: Fri Apr 29, 2005 3:23 pm

Post by hanji »

Any clues from apache's error_log?

hanji
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

hanji wrote:Any clues from apache's error_log?

hanji
Just checking now, although we've established it's PHP not apache because it crashes on command line too :?

I got him to make this code and tell me what happens (it gave the same results -- crashing):

Code: Select all

class myClass
{
    private static $foo = null;
    
    public static function setFoo($foo)
    {
        self::$foo = $foo;
    }

    public static function getFoo()
    {
        return self::$foo;
    }
}

echo myClass::getFoo();

myClass::setFoo(42);

echo myClass::getFoo();
So then he tried placing sleep(60) right at the very end of the script, immediately after *all* PHP code in the file like this:

Code: Select all

<?php

class myClass
{
    private static $foo = null;
    
    public static function setFoo($foo)
    {
        self::$foo = $foo;
    }

    public static function getFoo()
    {
        return self::$foo;
    }
}

echo myClass::getFoo();

myClass::setFoo(42);

echo myClass::getFoo();

sleep(60);
It all executes, and then 60 seconds later it crashes. This *must* be a PHP bug right? Lemme check. I've never seen anything like it.
Post Reply