Posted By

geek4evr on 04/14/11


Tagged

php object template oo


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

geek4evr


Simple Object Oriented Application Template


 / Published in: PHP
 

  1. <?php
  2. class App {
  3. var $dead = true;
  4. var $errs = array();
  5. var $output = "";
  6. var $init = false;
  7.  
  8. function errs(){
  9. foreach($this->errs as $err){
  10. echo "Message: $err[msg], Line: $err[line], Error Code: $err[code]";
  11. }
  12. }
  13. private function err($msg, $line, $number) { $this->errs[] = array("msg"=>$msg, "line"=>$line, "code"=>$number); }
  14. private function kill() { $this->dead = false; }
  15. public function out($in) { if($this->dead) $this->output .= $in; }
  16. private function setOut($in, $kill=false) { if($this->dead) $this->output = $in; if($kill) $this->kill(); }
  17. function App(){
  18. $this->init = true;
  19. if(method_exists($this, 'main')){
  20. $this->main();
  21. }
  22. $this->output();
  23. }
  24. function output(){ echo $this->output; }
  25. }
  26.  
  27. /* EXAMPLE USAGE: */
  28. function init(){
  29. $App = new App; // It will output and handle all the rest
  30. }
  31. class MyGreatAndAwesomeApp extends App {
  32. function main(){
  33. $this->out('A'); // add to output...
  34. $this->out(' very'); // ...
  35. $this->out(' cool'); // ...
  36. $this->out(' app!'); // ... A very cool app!
  37. $this->kill(); // no more output
  38. $this->out('No ouput here'); // even here
  39. /*
  40. Also:
  41. $this->setOut('This is the only output - overrides other stuff...');
  42. $this->setOut('Set output - then kill.', true);
  43. $this->err('Error message, ', 40, 'sdk_082');
  44. msg line anything else
  45.  
  46. $this->errs(); - output all the errors
  47. */
  48. }
  49. }
  50. init();
  51. ?>

Report this snippet  

You need to login to post a comment.