Hey all,
I got the PHP Anthology book and LOVE it so far. However, I haven't got very far... I'm struggling in chapter two understanding the basic ideas of OOP. For instance, I'm going to quote some code that Harry used for an example... with questions that I am not understanding. Find ** for questions.
PHP Code:
<?php
// Page class
class Page {
// Declare a class member variable
// [color=black]**But why must this be done?[/color]
var $page;
var $title;
var $year;
var $copyright;
// The constructor function ---
//[color=black]** What exactly does this do [/color][color=black]and why is it important? **[/color]
function Page($title,$year,$copyright) {
// Assign values to member variables
$this->page = '';
$this->title = $title;
$this->year = $year;
$this->copyright = $copyright;
// Call the addHeader() method ---
//[color=black]**Why is this being called now [/color][color=black]when the function is actully below it?[/color]
$this->addHeader();
}
// Generates the top of the page
function addHeader() {
$this->page.=<<<EOD
<html>
<head>
<title>$this->title</title>
</head>
<body>
<h1 align="center">$this->title</h1>
EOD;
}
// Adds some more text to the page
function addContent($content) {
$this->page.=$content;
}
// Generates the bottom of the page
function addFooter() {
$this->page.=<<<EOD
<div align="center">© $this->year $this->copyright</div>
</body>
</html>
EOD;
}
// Gets the contents of the page
function get() {
// Keep a copy of the page with no footer
// [color=red][color=black]Still not understanding what this is doing?[/color][/color]
$temp = $this->page;
// Call the addFooter() method
$this->addFooter();
// Restore $page for the next call to get
$page = $this->page;
$this->page = $temp;
return $page;
}
}
// Instantiate the page class
$webPage=new Page('As Easy as it Gets',date('Y'),'Easy Systems Inc.');
// Add something to the body of the page
$webPage->addContent(
"<p align=\"center\">It's so easy to use!</p>\n" );
// Display the page
echo ( $webPage->get() );
?>
I'm really wanting to learn all of this, but am having a hard time understanding some basics.
THANKS!
Josh
Bookmarks