Announcement

Collapse
No announcement yet.

Obliviate missing endpage()'s

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [ redux ] Obliviate missing endpage()'s

    I have to admit it's been some time since I laid my hands on an MCCodes script, so best practices out the window.
    I would like to give MCCodes developers a useful tip.

    How many times haven't you missed a $h->endpage(); initialization? I know I have had my fair share of times.
    When it comes to output buffering for template systems, it's really a nuisance to work with.

    So, I've come up with a hack for it...
    MCCodes uses a class named headers for all it's templating needs, and using a bit of OOP knowledge one can automatically "close" a script that doesn't have the endpage() initialization present.

    It's called a destructor. It executes automatically every time the class is destroyed, or in human terms, when the script has finished executing.
    One can simply place the endpage() initialization in there, with a logical statement, so that you never get caught of guard by it again.

    So here's a little bit of code that could make your life easier... It works on all versions of MCCodes, to my knowledge that is.
    Code:
    <?php
    
    class headers {
        
       // simple $h->endpage missing fix 
       private $page_closed = false;
       
       function __destruct() {
          if (empty($this->page_closed)) 
             $this->endpage();
    
          return true;
       }
    
    // the rest of the script
    
       function endpage() {
          global $db,$c,$userid, $set,$ir;
    
    
          $this->page_closed = true;
    
          // rest of code
      }
    } // end of headers{}
    It might be useful to some, and not to others.
    But I just thought I would share this age old hack.

    Spudinski.
    Last edited by Spudinski; 02-20-2012, 10:18 PM. Reason: I = Fool.

  • #2
    Eh, I think you made a typo in the method name (__destruct?)... Otherwise, good stuff. The original code was not written with PHP 5 in mind, though, hence why something like this isn't already there.
    MCCodes Primary Developer
    http://mccodes.com/

    Twitter @Dabomstew

    Comment


    • #3
      Originally posted by Dabomstew View Post
      Eh, I think you made a typo in the method name (__destruct?)... Otherwise, good stuff. The original code was not written with PHP 5 in mind, though, hence why something like this isn't already there.
      Thanks for mentioning that silly mistake of mine.

      Even if it was written with PHP5 in mind, I get why something like this is not implemented.
      My best guess would be conforming to a specific coding style.

      But, please be aware that this is not to discredit MCCodes, it's the other way around actually.
      This is just a simple hack I mostly use on development environments, it's a short-cut method of doing things that isn't possible with the current MCCodes architecture.

      Comment

      Working...
      X