[PHP] Include files and Initialize all classes in just a few lines!

Results 1 to 10 of 10
  1. #1
    Account Upgraded | Title Enabled! Hexadecimal is offline
    MemberRank
    Dec 2010 Join Date
    424Posts

    [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


  2. #2
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    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?

  3. #3
    Account Upgraded | Title Enabled! Hexadecimal is offline
    MemberRank
    Dec 2010 Join Date
    424Posts

    Re: [PHP] Include files and Initialize all classes in just a few lines!

    Quote Originally Posted by AngraMainyu View Post
    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.

  4. #4
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    Re: [PHP] Include files and Initialize all classes in just a few lines!

    Quote Originally Posted by Apixenz View Post
    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?

  5. #5
    Account Upgraded | Title Enabled! Hexadecimal is offline
    MemberRank
    Dec 2010 Join Date
    424Posts

    Re: [PHP] Include files and Initialize all classes in just a few lines!

    Quote Originally Posted by AngraMainyu View Post
    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.

  6. #6
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    Re: [PHP] Include files and Initialize all classes in just a few lines!

    Quote Originally Posted by Apixenz View Post
    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?

  7. #7
    Account Upgraded | Title Enabled! Hexadecimal is offline
    MemberRank
    Dec 2010 Join Date
    424Posts

    Re: [PHP] Include files and Initialize all classes in just a few lines!

    Quote Originally Posted by AngraMainyu View Post
    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.

  8. #8
    Infraction Baɴɴed holthelper is offline
    MemberRank
    Apr 2008 Join Date
    1,765Posts

    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

  9. #9
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    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.
    Last edited by s-p-n; 21-11-11 at 05:11 AM.

  10. #10
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    Re: [PHP] Include files and Initialize all classes in just a few lines!

    Quote Originally Posted by s-p-n View Post
    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.



Advertisement