views:

58

answers:

4

These are the class definitions

<?php
  abstract class MyTemplate {

  protected $arrayOfSpaces;
  protected $arrayOfVariables;
  protected $output;

  protected abstract function __construct(); 

  function outputHTML(){
    echo $output; //Apparently, the problem is HERE. <<<<>>>>>
  }
}
  class MyTemplateMain extends MyTemplate {
   function __construct(){
     $this->output="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
             \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"&gt;
             <html>
             <head>
             </head>
             <body>
             I love Rock n Roll!!!
             </body>
             </html>";
    }

  }
?>

And this is where I launch this page

<?php 
  require_once("view/templates.php");

  $page=new MyTemplateMain();
  $page->outputHTML();



?>

Doesn't work, though. Just shows a blank page, without the String I love rock n roll which was supposed to appear in the body.

I'm sure there are better ways to implement templates but I just want to figure out why this particular example doesn't work

Any help is appreciated. Thanks

PS: The quotes are all duly escaped and the file paths are ok too

+2  A: 

your syntax is weird, try this

  abstract class MyTemplate {

  protected $arrayOfSpaces;
  protected $arrayOfVariables;
  protected $output;

  public abstract function __construct(); 

  function outputHTML(){
    echo $this->output;
  }
}
  class MyTemplateMain extends MyTemplate {
    public function __construct(){
     $this->output="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
             \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"&gt;
             <html>
             <head>
             </head>
             <body>
             I love Rock n Roll!!!
             </body>
             </html>";
    }

  }

$page=new MyTemplateMain();
$page->outputHTML();
Hannes
Hmm you mean everything in one file? That shouldn't be the source of the problem, but Ok. Done that. STill doesn't work.. I'm thinking I must have overlooked some tiny detail..Thx anyway
Felipe Almeida
no sorry :) you got me wrong, the file wasn't the Problem (but i did it in one to save time). First i don't know any OOP Angle PHP5 (or 5.3) has, but i mainly changed the way you access the variables ($this->output insteadt of just $output or this.$output ) and changed the __constructur to public
Hannes
I think I solved it. I guess you must name the function ABSTRACT PROTECTED rather than PROTECTED ABSTRACT. Apparently, the "abstact" keyword comes first
Felipe Almeida
thats better coding style, but for the parser (as far as i know and tested it under pgp 5.3) it doesn't make a difference
Hannes
A: 

try $base->output instead of $this->output.

elsni
A: 

Ok. I don't know why but now it's working. I rewrote it some times so I must've done some correctly even though unintended. Here is the code if anyone wants to have alook and compare the versions. Thanks all

abstract class MyTemplate {

protected $arrayOfSpaces;
protected $arrayOfVariables;
protected $output;


function outputHTML(){
    echo $this->output;
}

}

class MyTemplateMain extends MyTemplate {

function __construct(){
    $this->output="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
             \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"&gt;
             <html>
             <head>
             </head>
             <body>
             I love Rock n Roll!!!
             </body>
             </html>";
}
}
Felipe Almeida
if first example in `outputHTML` function you have `echo $output;` which echo undefined variable and in second you have `echo $this->output;` which output object field.
jcubic
the problem is not the order of abstract and protected, its the handling of variables in class context
Hannes
+2  A: 

change :

 function outputHTML(){
    echo $output;
  }

to :

 function outputHTML(){
    echo $this->output;
  }
ts
Hm... PHP can't guess which variable I'm talking about if i don't use $this? Or is this just stylewise?
Felipe Almeida
PHP doesn't even try to guess. `$this` is always required.
Vegard Larsen
php doesn't ... guess
Hannes
in the context of object, $this always referes to, well... that object. But you can only access it from the inside of the object.
ts