Fork me on GitHub

Include, include_once, require and require_once in pn core  Bottom

  • Hi guys

    This is somehow a selfquestion about PN code... I was looking at the pnInit code, and found there are lots of simple "include" for classes.

    Let's suppose for some strange bad luck somebody goes and does the same include on his/her modules... that's going to break the system - stop and die in lots of servers... "Can't redeclare class blah...", being also complicated to trace as maybe the class is included in another class which is refered from another file, well, you know.

    So the question is, wouldn't it be better to apply a bit of "selfdeffensive" code and try to make it complicated to get broken, by, let's say, change the include directives with include_once ?

    I think it's more meaningful, in the way that it makes more sense to ask a file to be included once during the execution of postnuke.

    The same goes for example for some files where there are several includes in the middle of the code. Something like

    Code

    if (...) {
      include(some class)
    }


    Trying to know which files are depending of each others with this style of coding is terribly difficult. I don't think the overhead is going to be excessive if that include is always at the top of the file, and it would improve readability and hence PN code quality.

    So what do you think of these self-questions?
  • Something like this would be good:

    Code

    if (!class_exists('myclass'))
        {
            require_once('myclass.php');   
        }

    (for classes)

    Actually this is a valid point and module developers should put this check into their modules if using 3rd party classes to extend functionality. Basically if they are modifying the class it's self they should rename the class, otherwise do the above check that I mentioned...

    --
    -Lobos
    Professional PHP Framework Services: Concept, Development and Deployment
  • That's a very good point. But that one has relatively easier solution: if you develop your custom classes, prepend their names with your prefix. That way is harder to have a name collision!!

    In any case, for classes like Smarty, that practice would be very interesting. We could have it at the beginning of the code, like:

    Code

    if(!class_exists("Smarty")) {
       require_once(... appropiatepath to smarty.class.php);
    }


    Going further, it would be even nicer if we add something in the class definitions like the C/C++ style. Something like the classical C structure:

    Code

    #ifndef thatclass
    #define thatclass

    here goes the class header

    #endif


    Have you ever thought about doing this in PHP? I'm not very sure about how that could be made...

This list is based on users active over the last 60 minutes.