crabfinger Posted January 3, 2011 Share Posted January 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/223310-lazy-intelligent-class-loading/ Share on other sites More sharing options...
trq Posted January 4, 2011 Share Posted January 4, 2011 Take a look at __autoload. Quote Link to comment https://forums.phpfreaks.com/topic/223310-lazy-intelligent-class-loading/#findComment-1154425 Share on other sites More sharing options...
crabfinger Posted January 4, 2011 Author Share Posted January 4, 2011 Take a look at __autoload. i was thinking about that too but this wouldn't replace autoload in any way. It could make a useful addition to __autoload in some applications Quote Link to comment https://forums.phpfreaks.com/topic/223310-lazy-intelligent-class-loading/#findComment-1154430 Share on other sites More sharing options...
trq Posted January 4, 2011 Share Posted January 4, 2011 I'm really not sure there's anything that useful about it actually. It's just going to make your code harder to read. Sorry, but what exactly are you trying to achieve? Quote Link to comment https://forums.phpfreaks.com/topic/223310-lazy-intelligent-class-loading/#findComment-1154434 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.