Is it only me who gets this problem? Static class properties

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

Is it only me who gets this problem? Static class properties

Post by Chris Corbyn »

On some machines, when I make classes which contain static properties such as a singleton or a static registry PHP chokes and forces download of index.php.

WTF is going on? I've had other people complain in the past and neither of us have been able to see a reason for it and this has happened to me on at least 4 different machines now so it can't just be me.

Can anyone see why this singleton should be choking?

Code: Select all

<?php

/**
 * Defines the methods any actual locators must implement
 * @package SHANE
 * @author Chris Corbyn
 */
abstract class Locator
{
	/**
	 * Inform of whether or not the given class can be found
	 * @param string class
	 * @return bool
	 */
	public function canLocate($class)
	{
		return file_exists($this->getPath($class));
	}
	/**
	 * Get the path to the class
	 * @param string class
	 * @return string
	 */
	abstract public function getPath($class);
}

/**
 * The main service locator.
 * Uses loosely coupled locators in order to operate
 * @package SHANE
 * @author Chris Corbyn
 */
class Autoloader
{
	/**
	 * A singleton instance
	 * @var Autoloader
	 */
	protected static $instance = null;
	/**
	 * Contains any attached service locators
	 * @var array Locator
	 */
	protected $locators = array();
	/**
	 * Constructor kept protected
	 */
	protected function __construct() {}
	
	/**
	 *Singleton factory method
	 * @return AutoLoader
	 */
	public static function instance()
	{
		if (self::$instance === null) self::$instance = new Autoloader();
		return self::$instance;
	}
	/**
	 * Attach a new type of locator
	 * @param object Locator
	 * @param string key
	 */
	public function attach(Locator $locator, $key)
	{
		$this->locators[$key] = $locator;
	}
	/**
	 * Remove a locator that's been added
	 * @param string key
	 * @return bool
	 */
	public function drop($key)
	{
		if ($this->isActive($key))
		{
			unset($this->locators[$key]);
			return true;
		}
		else return false;
	}
	/**
	 * Check if a locator is currently loaded
	 * @param string key
	 * @return bool
	 */
	public function isActive($key)
	{
		return array_key_exists($key, $this->locators);
	}
	/**
	 * Load in the required service by asking all service locators
	 * @param string class
	 */
	public function load($class)
	{
		foreach ($this->locators as $key => $obj)
		{
			if ($obj->canLocate($class))
			{
				require_once $obj->getPath($class);
				if (class_exists($class)) return;
			}
		}
	}
}

/**
 * PHPs default __autoload
 * Just a wrapper to Autoloader::load()
 * @package SHANE
 * @author Chris Corbyn
 * @param string class
 */
function __autoload($class)
{
	Autoloader::instance()->load($class);
	if (!class_exists($class) && !interface_exists($class))
	{
		throw new ShaneException('Class "' . $class . '" cannot be loaded');
	}
}

Code: Select all

Autoloader::instance()->attach(new Locator_DotClass(), "DOTCLASS");
The most bizarre thing is that it doesn't happen all the time. I can sit there refreshing the page and 2 out of 10 pages or so will actually load correctly.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ignore this. It was my own stupidity. I was calling a function which ultimately calls itself again and thus was running out of memory :oops:
Post Reply