[PHP] Include files and Initialize all classes in just a few lines!
First release here in Coders' Paradise, but this was a pretty cool thing I just made, and I really didn't think it would work, but it actually did.
This will allow you to place multiple files inside an array, and class names inside another array. Using foreach, it'll grab all the files, include them, then initialize all classes needed to be initialized in the array.
Code:
PHP Code:
<?php
$IncludeFiles = array("class.mysql.php", "class.user.php");
foreach($IncludeFiles AS $File)
require_once($File);
$InitClass = array("SQL", "User");
foreach($InitClass AS $Class)
${$Class} = new $Class();
echo $User->say("Hi <br/>");
echo $SQL->Count("select * from mysql.user");
?>
Proof of Concept:
Screenshot by Lightshot
Enjoy
-BGxApixen
Re: [PHP] Include files and Initialize all classes in just a few lines!
How many singletons are you using that it becomes necessary to instantiate an entire array of them?
Re: [PHP] Include files and Initialize all classes in just a few lines!
Quote:
Originally Posted by
AngraMainyu
How many singletons are you using that it becomes necessary to instantiate an entire array of them?
Not many. I just thought it was a nice thing to code up. I'm making about 4 classes. Instead of making $Var = new Foo(); every time, just gonna make an array, and allow it to initialize itself. It's a pretty easy concept, is it not? Easy to edit.
Re: [PHP] Include files and Initialize all classes in just a few lines!
Quote:
Originally Posted by
Apixenz
Not many. I just thought it was a nice thing to code up. I'm making about 4 classes. Instead of making $Var = new Foo(); every time, just gonna make an array, and allow it to initialize itself. It's a pretty easy concept, is it not? Easy to edit.
Is there something wrong with static classes?
Re: [PHP] Include files and Initialize all classes in just a few lines!
Quote:
Originally Posted by
AngraMainyu
Is there something wrong with static classes?
There's no reason to use Static classes all the time. From what I think, using static classes all the time could be a bad idea.
Re: [PHP] Include files and Initialize all classes in just a few lines!
Quote:
Originally Posted by
Apixenz
There's no reason to use Static classes all the time. From what I think, using static classes all the time could be a bad idea.
What do you gain from using singletons instead?
Re: [PHP] Include files and Initialize all classes in just a few lines!
Quote:
Originally Posted by
AngraMainyu
What do you gain from using singletons instead?
It's cleaner. And to me, the best thing to use. I haven't seen, ever, one website that uses all static callings for the whole OOP process.
Re: [PHP] Include files and Initialize all classes in just a few lines!
pointless but on the right track. load only the file need to work properly.
EDIT:
i use this for the time being:
PHP Code:
function getClass($className) {
$include = "./Module/Module.{$className}.php";
if(file_exists($include))
require_once($include);
return new $className(self::$instance);
}
thread jacked xD
Re: [PHP] Include files and Initialize all classes in just a few lines!
You can just use autoload and you don't have to require the file to each class.. You really don't need getClass() for that.
PHP Code:
function __autoload( $class )
{
require_once( './classes/' . $class . '.php' );
}
If you don't need to instantiate classes with magic methods or use 'extend', you can pretty much use static.. (some exceptions, but.. not much) Just because people on the web codes OO poorly doesn't mean you should. Static classes are around for a reason.
Re: [PHP] Include files and Initialize all classes in just a few lines!
Quote:
Originally Posted by
s-p-n
Just because people on the web codes OO poorly doesn't mean you should. Static classes are around for a reason.
Thank you.
In virtually all cases, eager loading will be significantly slower than pairing __autoload with APC's lazy loading anyway.