[转]类与PHP(IV)
时间:2007-02-17 来源:PHP爱好者
Classes and PHP
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
This function will take a message passed in the argument and print it out in the appropriate style object. So to print a message we can:
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<?php
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Basic->TextOut('This is my test message');
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Tbody->TextOut(' -- kinda neat, huh?');
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
?>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
Notice, there are no <BR> between the two function calls so they will print on the same line. Also, I just wanted a smaller font for the second part of the output and I had already declared that in $Tbody so I used that. This is safe in this instance as the only other difference between $Basic and $Tbody is "bgcol" and that isn't used in this function. Notice the " " in the function declaration? That is there so if no message is passed the function will print out a non- breaking space. Why will become clear later on.
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
So far we haven't saved a lot of work. The last example is easier if you want to change font color and/or size in the middle of a sentence but still doesn't justify writing an entire class. How about we add to the functions:
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<?php
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
function TDOut ($message=" ",$colspan=1) {
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
PRINT "<TD COLSPAN=$colspan BGCOLOR="$this->bgcol" ".
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
"ALIGN="$this->align" VALIGN="$this->valign">";
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$this->TextOut($message);
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
PRINT "</TD>n";
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
}
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
?>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
Now, we're getting somewhere! Remember, I wanted to have different background colors for my tables. Now I can do this:
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<TABLE>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<TR>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<?php
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("Name",2);
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("Location",3);
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
?>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
</TR>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<TR>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<?php
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("Last");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("First");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("City");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("State/Province");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("Country");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
?>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
</TR>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
There. See how the colspan argument works. If it's not declared it defaults to 1. So in the first table row "Name" spans 2 columns and "Location" spans 3. In the second row all of them cover a single column.
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
php爱好者站 http://www.phpfans.net c/vc/c++/java.
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
This function will take a message passed in the argument and print it out in the appropriate style object. So to print a message we can:
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<?php
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Basic->TextOut('This is my test message');
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Tbody->TextOut(' -- kinda neat, huh?');
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
?>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
Notice, there are no <BR> between the two function calls so they will print on the same line. Also, I just wanted a smaller font for the second part of the output and I had already declared that in $Tbody so I used that. This is safe in this instance as the only other difference between $Basic and $Tbody is "bgcol" and that isn't used in this function. Notice the " " in the function declaration? That is there so if no message is passed the function will print out a non- breaking space. Why will become clear later on.
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
So far we haven't saved a lot of work. The last example is easier if you want to change font color and/or size in the middle of a sentence but still doesn't justify writing an entire class. How about we add to the functions:
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<?php
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
function TDOut ($message=" ",$colspan=1) {
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
PRINT "<TD COLSPAN=$colspan BGCOLOR="$this->bgcol" ".
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
"ALIGN="$this->align" VALIGN="$this->valign">";
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$this->TextOut($message);
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
PRINT "</TD>n";
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
}
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
?>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
Now, we're getting somewhere! Remember, I wanted to have different background colors for my tables. Now I can do this:
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<TABLE>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<TR>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<?php
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("Name",2);
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("Location",3);
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
?>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
</TR>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<TR>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
<?php
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("Last");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("First");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("City");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("State/Province");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
$Theader->TDOut("Country");
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
?>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
</TR>
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
There. See how the colspan argument works. If it's not declared it defaults to 1. So in the first table row "Name" spans 2 columns and "Location" spans 3. In the second row all of them cover a single column.
wwww.chin aitpower.comIjbAo50biyuhPDQZrxZFmv2wf
php爱好者站 http://www.phpfans.net c/vc/c++/java.
相关阅读 更多 +