Recent Topics

1 Mar 04, 2018 21:18    

function load_class( $class_path, $classname )
I was wondering why this function forces classes to be loaded in the $inc_path directory?

Please can dev considering allowing classes to be loaded from plugins?

If accepted, the class_load should auto detect the plugin_folder (Plugin) that requested the class to be loaded.

My proposed solution.

/**
 * In PHP4, this immediately loaded the class. In PHP5, it's smarter than that:
 * It only registers the class & file name so that PHP can later load the class
 * IF and ONLY IF the class is actually needed during execution.
 */
function load_class( $class_path, $classname, $relative_to = 'inc' )
{
	global $map_class_path;
	
	switch( $relative_to )
	{
		case 'plugins':// detect which plugin requested this, then load the class
			
			global $plugins_path;
			
				//PHP 4 >= 4.3.0, PHP 5, PHP 7
				$backtrace = debug_backtrace();

				foreach( $backtrace as $l_trace )
				{
					if( isset( $l_trace['file'] ) && isset( $l_trace['function'] ) && $l_trace['function'] == 'load_class' )
					{
						$sub_path = preg_replace( ':^'.preg_quote(  $plugins_path, ':' ).':', '', $l_trace['file'] );
						$path_parts = explode( '/', $sub_path );
						$plugin_folder = isset($path_parts[0]) ? $path_parts[0] : NULL;
						$source = trailing_slash( $plugins_path.$plugin_folder ).$class_path;
						break;
					}
				}
					// This is a class loader, make sure $Debuglog exists already
					if( ! empty( $Debuglog ) )
					{
						if( ! file_exists( $source ) )
						{
							$source = trailing_slash( $plugins_path.$plugin_folder ).$class_path;
							$Debuglog->add( sprintf( T_('file [%s] does not exist'), $source ), 'Load class' );

							// Last try
							$source = $plugins_path.$class_path;

							if( ! file_exists( $source ) )
							{
								$Debuglog->add( sprintf( T_('file [%s] does not exist'), $source ), 'Load class' );
								break;
							}

						}
						else
						{
							$Debuglog->add( sprintf( T_('Class [%s] load is requested from plugin %s'), $classname, $source_folder ), 'Load class' );
							$Debuglog->add( sprintf( T_('Class [%s]: %s'), $classname, $source ), 'Load class' );
						}
					}

			break;
			
		default: // The normal way that is backwards compatible
			
			global $inc_path;
				
			$source = $inc_path;
			
			break;	
	}
	
	if( !is_null($classname) )
	{
		$map_class_path[strtolower($classname)] = $source.$class_path;
	}
	return true;
}

2 Mar 08, 2018 02:51

I don't actually want plugins to rely on this function because we might remove it in the future.

3 Mar 08, 2018 17:45

Thank you for responding


Form is loading...