Jump to content

Lazy (Intelligent?) class loading


crabfinger

Recommended Posts

I'm not sure why I did this just yet but I'm sure I'll figure it out soon enough. Mostly I got sidetracked on a search for Something in the PHP documentation and was intrigued by a couple pages. Sorry, I can't seem to find the specific pages in my history. Anyways if you want to have more control over how classes are loaded in user contributed modules inserted into your application this may help

<?php
function loadClass( $strClass, $aryArgs = array(), &$varRef = NULL ) {
	// If a reference variable is not defined then set it to the class name.
	if( $varRef === NULL ) {
		global $$strClass;
		$varRef = &$$strClass;
	}
	// If the reference variable is not empty we cant very well declare something in it.
	if( empty( $varRef ) ) {
		// Not the prettiest piece of code in the world but i couldn't find a prettier way.
		eval('$varRef = new $strClass(\'' . implode('\',\'',$aryArgs) . '\');');
		return true; 
	} else {
		return false;
	}
}
?>

Well how the hell does this help me you ask?

<?php
// a couple example classes
class myClass {
	var $strArgs = NULL;
	function __construct() {
		$this->strArgs = preg_replace('/\s*/m','',preg_replace('/\n*/m','',print_r(func_get_args(),true)));
	}
	function foo() {
		return 'hello';
	}
}

class myNewClass extends myClass {

}

print "\r\n";
// This works, 
// you can declare the variable before-hand but you dont have to. If you do it has to be empty and not null or you will have unexpected results
// $myClass = '';
print "\r\n";
var_dump( loadClass( 'myClass' ) ); 
var_dump( $myClass );
var_dump( $myClass->foo() );
// This will return false and not load the class but continue with the script this saves from classes being created multiple times without our knowledge
print "\r\n";
var_dump( loadClass( 'myClass' ) );
// So if we want another one we have to specify a variable to load the class into
// This way you have to declare the variable before-hand or the function will see it as null and try to set it as the class name
$myClass2 = '';
print "\r\n";
var_dump( loadClass( 'myClass', array(), $myClass2 ) ); 
var_dump( $myClass2 );
var_dump( $myClass2->foo() );
// another way of doing it
$strClass = 'myNewClass';
print "\r\n";
var_dump( loadClass( $strClass ) );
var_dump( $$strClass );
var_dump( $$strClass->foo() );
// Here we make full use of loadClass
$strClass = 'myNewClass';
$aryArgs = array( 'foo', 'bar' );
$varRef = $strClass . '2';
$$varRef = '';
print "\r\n";
var_dump( loadClass( 'myNewClass', $aryArgs , $$varRef ) );
var_dump( $$varRef );
var_dump( $$varRef->foo() );
php?>

This should output something similar to

bool(true)
object(myClass)#1 (1) {
  ["strArgs"]=>
  string(12) "Array([0]=>)"
}
string(5) "hello"

bool(false)

bool(true)
object(myClass)#2 (1) {
  ["strArgs"]=>
  string(12) "Array([0]=>)"
}
string(5) "hello"

bool(true)
object(myNewClass)#3 (1) {
  ["strArgs"]=>
  string(12) "Array([0]=>)"
}
string(5) "hello"

bool(true)
object(myNewClass)#4 (1) {
  ["strArgs"]=>
  string(23) "Array([0]=>foo[1]=>bar)"
}
string(5) "hello"

Just hoping to intrigue someone into helping me make this into something useful

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.