Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts

    Advice with a class to load modules please?

    Hello. I'm after some help please. I'm trying to build a framework to maintain my website structure.

    On my website my layout consists of a main frame and a right hand menu.

    In the right hand menu I have things like

    * a most read box
    * a contact me graphic
    * a social media box

    In my framework I'm thinking how I can load each of them in, so I thought of renaming the files and putting them in their own .php file

    * mostread.php
    * graphic.php
    * socialmedia.php

    Then calling them in like so on the pages I need them

    PHP Code:
    class modules {
      Public function 
    __construct(array $array) {
        foreach(
    $array as $filename) {
          include_once(
    $filename.'.php');
        }
     }

    PHP Code:
    $array = array('mostread','graphic','socialmedia');
    $obj = new modules($array); 
    Would this be a good way to do this, or is there a better way? I can't help but feel I'm missing a trick with using magic methods here. Thank you.
    Last edited by Oatley; 05-15-2013 at 03:10 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Are they class files or procedural files?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Oatley (05-15-2013)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts
    Just procedural. Thank you

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That looks to be a fairly reasonable idea overall then. You may want to double check that everything in the array is a file that's readable (maybe even check that its in a place you're allowing to be read from), then provide it as an argument.
    An alternative is to use a single entry point, and provide querystring information regarding it. That's probably more along what I'd do, simply to save on processing files. I typically build lots and lots of classes, but only one executable file with only a few lines of code. Too bad PHP doesn't have a class entry point.

    If these were classes, than the better approach is to use autoloading with registered extensions. That's a simple matter of adding an include path, and registering the extension (which is why I always suggest .class.php as your php class files and interfaces). Then it's automatically discovered in the include path and included on invocation of new:
    PHP Code:
    set_include_path(get_include_path() . PATH_SEPARATOR '/path/to/class/files'); // you can do this as many times as you want for multiple locations
    spl_autoload_extensions('.class.php');
    spl_autoload_register();

    new 
    Class1();
    new 
    clazz();
    // etc etc 
    Works with use commands as well so long as the fileystem structure matches the namespace. So then I can use \namespace\subnamespace\MyClass or invoke new \namespace\subnamespace\myclass and it automatically resolves so long as the parent directory to \namespace is registered. Neato.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Oatley (05-15-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •