Page 1 of 1

New to Classes and having problems

Posted: Tue Feb 24, 2009 2:21 pm
by yorkcoparamedic
Hi all! Well I have done several net searches and forum searches for my answer. I have found some, but nothing that helps me understand what it is that I'm doing wrong.

I am fairly new to PHP but do understand most of the basics. I also have a very good grasp of x/html as well. I just purchased the book PHP in Action - Ojects, Design, Agility. I was working with some of the first examples and everything was going fine until Inheritance and Extend came along.

Here is my code that I am trying:

Code: Select all

 [color=#FF0000]<?php[/color]
[color=#408000]class[/color] HtmlDocument {
[color=#0000FF]function[/color] getHtml() {
[color=#408000]return[/color] [color=#FF0000]"[/color]<html><body>[color=#FF0000]"[/color] . [color=#00FFFF]$this[/color]->getContent() . [color=#FF0000]"[/color]</body></html>[color=#FF0000]"[/color];
}
 
[color=#0000FF]function[/color] getContent() { [color=#408000]return[/color][color=#FF0000] ''[/color]; }
 
}
 
[color=#408000]class[/color] HelloWorld [color=#408000]extends[/color] HtmlDocument {
[color=#408000]public [/color]$world;
[color=#0000FF]function[/color] __construct($world) {
[color=#00FFFF]$this[/color]->name = $world;
}
 
[color=#0000FF]function[/color] getContent() {
[color=#008000]return[/color] [color=#FF0000]"[/color]Hello, [color=#FF0000]"[/color].[color=#00FFFF]$this[/color]->name .[color=#FF0000]"[/color]![color=#FF0000]"[/color];
}
}
 
$greet = [color=#408000]new[/color] HelloWorld;
[color=#0000FF]echo[/color] $greet->getHtml();
 
[color=#FF0000]?>[/color]
 
I keep getting this error now matter what i change or do to the script.
 

Code: Select all

 
Warning: Missing argument 1 for HelloWorld::__construct(), called in /Users/charles/Sites/Testing PHP/index.php on line 26 and defined in /Users/charles/Sites/Testing PHP/index.php on line 15
Hello, !
 
I understand that the child class will inherit everything from the parent and then can be further changed on its own to add extended functionality. 1- I am missing an argument. 2- Not sure extactly how to call classes that have been extended.
Any help would be nice. Thanks in advance!

Re: New to Classes and having problems

Posted: Wed Feb 25, 2009 5:11 am
by susrisha

Code: Select all

 
LINE NUMBER ON/OFF | EXPAND/CONTRACT
 <?php
class HtmlDocument {
function getHtml() {
return "<html><body>" . $this->getContent() . "</body></html>";
}
 
function getContent() { return ''; }
 
}
 
class HelloWorld extends HtmlDocument {
[color=#FF0000]public $world; //this line is where the error lies[/color]
//change to 
public $name;
 
function __construct($world) {
$this->name = $world;
}
 
function getContent() {
return "Hello, ".$this->name ."!";
}
}
 
[color=#FF0000]$greet = new HelloWorld;//declaration did not have the name with it..[/color]
 
$greet = new HelloWorld('yorkcoparamedic');
echo $greet->getHtml();
 
?>
 

Re: New to Classes and having problems

Posted: Wed Feb 25, 2009 7:13 pm
by yorkcoparamedic
Thank you for the quick answer. It make sense now.

Thanks again,
Charles