note 73530 added to language.oop5.decon
| From: | DavidSoussan at osu1 dot php dot net | Date: | Wed, 28 Feb 2007 07:08:47 +0000 |
| Subject: | note 73530 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123940@lists.php.net to get a copy of this message | ||
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
----
Server IP: 194.145.210.3
Probable Submitter: 89.240.139.48
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/73530
Del: integrated -- https://master.php.net/note/delete/73530/integrated
Del: useless -- https://master.php.net/note/delete/73530/useless
Del: bad code -- https://master.php.net/note/delete/73530/bad+code
Del: spam -- https://master.php.net/note/delete/73530/spam
Del: non-english -- https://master.php.net/note/delete/73530/non-english
Del: in docs -- https://master.php.net/note/delete/73530/in+docs
Del: other reasons-- https://master.php.net/note/delete/73530
Reject -- https://master.php.net/note/reject/73530
Search -- https://master.php.net/manage/user-notes.php