讀《PHP和MySQL Web開發》筆記合集: php
http://my.oschina.net/bluefly/blog/478580 html
一、面向對象和類 web
public function DisplayStyles()
{
?>
<style>
h1 {
color:white; font-size:24pt; text-align:center;
font-family:arial,sans-serif
}
.menu {
color:white; font-size:12pt; text-align:center;
font-family:arial,sans-serif; font-weight:bold
}
td {
background:black
}
p {
color:black; font-size:12pt; text-align:justify;
font-family:arial,sans-serif
}
p.foot {
color:white; font-size:9pt; text-align:center;
font-family:arial,sans-serif; font-weight:bold
}
a:link,a:visited,a:active {
color:white
}
</style>
<?php
}
|
<?php
class ObjectIterator implements Iterator {
private $obj;
private $count;
private $currentIndex;
function __construct($obj)
{
$this->obj = $obj;
$this->count = count($this->obj->data);
}
function rewind()
{
$this->currentIndex = 0;
}
function valid()
{
return $this->currentIndex < $this->count;
}
function key()
{
return $this->currentIndex;
}
function current()
{
return $this->obj->data[$this->currentIndex];
}
function next()
{
$this->currentIndex++;
}
}
class Object implements IteratorAggregate
{
public $data = array();
function __construct($in)
{
$this->data = $in;
}
function getIterator()
{
return new ObjectIterator($this);
}
}
$myObject = new Object(array(2, 4, 6, 8, 10));
$myIterator = $myObject->getIterator();
for($myIterator->rewind(); $myIterator->valid(); $myIterator->next())
{
$key = $myIterator->key();
$value = $myIterator->current();
echo $key." => ".$value."<br />";
}
?>
|
<?php
require_once("page.inc");
$class = new ReflectionClass("Page");
echo "<pre>".$class."</pre>";
?>
|
<?php
class Page
{
// class Page's attributes
public $content;
public $title = "TLA Consulting Pty Ltd";
public $keywords = "TLA Consulting, Three Letter Abbreviation,
some of my best friends are search engines";
public $buttons = array("Home" => "home.php",
"Contact" => "contact.php",
"Services" => "services.php",
"Site Map" => "map.php"
);
// class Page's operations
public function __set($name, $value)
{
$this->$name = $value;
}
public function Display()
{
echo "<html>\n<head>\n";
$this -> DisplayTitle();
$this -> DisplayKeywords();
$this -> DisplayStyles();
echo "</head>\n<body>\n";
$this -> DisplayHeader();
$this -> DisplayMenu($this->buttons);
echo $this->content;
$this -> DisplayFooter();
echo "</body>\n</html>\n";
}
public function DisplayTitle()
{
echo "<title>".$this->title."</title>";
}
public function DisplayKeywords()
{
echo "<meta name=\"keywords\"
content=\"".$this->keywords."\"/>";
}
public function DisplayStyles()
{
?>
<style>
h1 {
color:white; font-size:24pt; text-align:center;
font-family:arial,sans-serif
}
.menu {
color:white; font-size:12pt; text-align:center;
font-family:arial,sans-serif; font-weight:bold
}
td {
background:black
}
p {
color:black; font-size:12pt; text-align:justify;
font-family:arial,sans-serif
}
p.foot {
color:white; font-size:9pt; text-align:center;
font-family:arial,sans-serif; font-weight:bold
}
a:link,a:visited,a:active {
color:white
}
</style>
<?php
}
public function DisplayHeader()
{
?>
<table width="100%" cellpadding="12"
cellspacing="0" border="0">
<tr bgcolor ="black">
<td align ="left"><img src = "logo.gif" /></td>
<td>
<h1>TLA Consulting Pty Ltd</h1>
</td>
<td align ="right"><img src = "logo.gif" /></td>
</tr>
</table>
<?php
}
public function DisplayMenu($buttons)
{
echo "<table width=\"100%\" bgcolor=\"white\"
cellpadding=\"4\" cellspacing=\"4\">\n";
echo "<tr>\n";
//calculate button size
$width = 100/count($buttons);
while (list($name, $url) = each($buttons)) {
$this -> DisplayButton($width, $name, $url,
!$this->IsURLCurrentPage($url));
}
echo "</tr>\n";
echo "</table>\n";
}
public function IsURLCurrentPage($url)
{
if(strpos($_SERVER['PHP_SELF'], $url )==false)
{
return false;
}
else
{
return true;
}
}
public function
DisplayButton($width,$name,$url,$active = true)
{
if ($active) {
echo "<td width = \"".$width."%\">
<a href=\"".$url."\">
<img src=\"s-logo.gif\" alt=\"".$name."\" border=\"0\" /></a>
<a href=\"".$url."\"><span class=\"menu\">".$name."</span></a>
</td>";
} else {
echo "<td width=\"".$width."%\">
<img src=\"side-logo.gif\">
<span class=\"menu\">".$name."</span>
</td>";
}
}
public function DisplayFooter()
{
?>
<table width="100%" bgcolor="black" cellpadding="12" border="0">
<tr>
<td>
<p class="foot">© TLA Consulting Pty Ltd.</p>
<p class="foot">Please see our <a href ="">legal
information page</a></p>
</td>
</tr>
</table>
<?php
}
}
?>
|
Class [ class Page ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 2-152 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [4] { Property [ public $content ] Property [ public $title ] Property [ public $keywords ] Property [ public $buttons ] } - Methods [10] { Method [ public method __set ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 16 - 19 - Parameters [2] { Parameter #0 [ $name ] Parameter #1 [ $value ] } } Method [ public method Display ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 21 - 33 } Method [ public method DisplayTitle ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 35 - 38 } Method [ public method DisplayKeywords ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 40 - 44 } Method [ public method DisplayStyles ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 46 - 74 } Method [ public method DisplayHeader ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 76 - 90 } Method [ public method DisplayMenu ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 92 - 107 - Parameters [1] { Parameter #0 [ $buttons ] } } Method [ public method IsURLCurrentPage ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 109 - 119 - Parameters [1] { Parameter #0 [ $url ] } } Method [ public method DisplayButton ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 122 - 136 - Parameters [4] { Parameter #0 [ $width ] Parameter #1 [ $name ] Parameter #2 [ $url ] Parameter #3 [ $active = true ] } } Method [ public method DisplayFooter ] { @@ D:\wamp\www\study\book\Chapter 06\page.inc 138 - 151 } } }
|