note 73530 deleted from language.oop5.decon by dmytton
| From: | dmytton@php.net | Date: | Wed, 28 Feb 2007 15:39:05 +0000 |
| Subject: | note 73530 deleted from language.oop5.decon by dmytton | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-123953@lists.php.net to get a copy of this message | ||
Note Submitter: David Soussan
Reason: useless
----
I am ABSOLUTELY AMAZED at how much time I have just wasted because nowhere is it obvious that the
constructor function name begins with TWO UNDERSCORES ????
_._construct
I mean literally hours and hours googling and reading articles and tutorials, none of which were any
use, all because I used ONE UNDERSCORE
_construct
On a further point, the examples, or lack of them, on this page give very little insight into the
proper use of a constructor, while the user notes are concerned with some very esoteric and arcane
points and arguments.
So for future newbies like me, here at least is one way to benefit from using them:
constructor function can save you lots of typing when using a class object because you can pass your
variables into the new object when you create it instead of having to set them with separate
instructions before you call a method
<?php
class MakeSortTable {
// first row holds class for css and table sort
private $toprow;
private $table;
private $caption;
public function __construct($id, $title) {
$this->table = '<table name="' . $id . '" id="' . $id .
'" class="sortable">';
$this->caption = '<caption>' . $title . ' - [click the title to sort by
that column]</caption>';
}
public function MakeTable($result) {
echo $this->table;
echo $this->caption;
$this->toprow = $result->fetch(PDO::FETCH_ASSOC);
$this->MakeTH();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$this->MakeRow($row);
}
echo '</table>';
}
private function MakeTH() {
foreach ($this->toprow as $key=>$val) {
echo '<th class="' . $val . '">' . $key .
'</th>';
}
echo '</th>';
}
private function MakeRow($row) {
echo '<tr>';
foreach ($row as $key=>$val) {
$cls = 'class="' . $this->toprow[$key] . '"';
echo '<td ' . $cls . '>' . $val . '</td>';
}
echo '</tr>';
}
}
// and you call it like this
$tbl = new MakeSortTable('summary','Summary of all Instruction');
$tbl->MakeTable($stmt);
// instead of
$tbl = new MakeSortTable();
$tbl->caption = '<caption>Summary of all Insructions</caption>';
$tbl->table = 'summary';
$tbl->MakeTable($pdo);
?>
Hope this helps someone