Hello All,
Have some more questions please. Having a problem with extends. I want to output a box with a heading in my first class (works fine)
But then in my second class want to use ‘extends’ to output a heading (from the class CssBox) AND some lipsum text below that. Here’s my code:
//* New Class which contains a box and a heading
Class CssBox {
protected $boxstyle;
protected $heading; //So should pass over using extends
function __construct($boxstyle) {
$this->boxstyle = $boxstyle;
}
function theHeading($heading) {
$this->heading = $heading;
}
function displayWholeBox() {
return "<div class='".$this->boxstyle."'>".$this->heading."</div>";
}
}
//* New Class using extends to grab that heading from the parent class (BUT NOT the box style) above class using extends and plonk some lipsum text underneath
class Lipsum extends CssBox {
private $lipsum;
function theText($lipsum) {
$this->lipsum = $lipsum;
}
function displayText() {
return "<div class='<h1>".$this->heading."</h1>\
<p>".$this->lipsum."</p>";
}
}
//* All outputs fine
$box1 = new CssBox("box_long");
$box1->theHeading("Hello Message");
echo $box1->displayWholeBox();
echo "<hr style='clear:both;margin-top:10px;' />";
//* Outputs the lipsum text but no heading and a error Missing argument 1 for CssBox::__construct()
$lipsum = new Lipsum();
$lipsum->theText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis porta semper nisl, non tempor felis imperdiet in. Cras feugiat, elit nec fermentum faucibus, lorem magna consequat eros, hendrerit placerat lacus ligula nec erat. Donec lacinia dolor at arcu tempor blandit. Proin mauris ipsum, porta vitae semper eu");
echo $lipsum->displayText();
?>
However i’m getting the message when I create my new instance $lipsum = new Lipsum();
Missing argument 1 for CssBox::__construct()
Now I dont see why I am getting that as when I create my instance of $lipsum = new Lipsum(); I am not asking for the constructor to be called from its parent class, just asking it to call $this->heading? So why do I have to put anything in the paranthasis here?
Also can you have a constructor in the extended class even if you have one in it’s parent class?
Thanks