我正在关注一本php书,并不了解一些功能。
具体来说,公共功能DisplayMenu($ buttons)。我知道这会为菜单创建一个表格(主页,联系人等,并将它们分开)。这里的参数
public function DisplayButton($width, $name, $url, $active = true)
$width = 100/count($buttons);
Here is what I am not understanding. Im setting a variable called width to be 100/count ? lets say buttons are 4 so 100/4 25? What is the point of the 25? Also,
$this -> DisplayButton($width, $name, $url, !$this->IsURLCurrentPage($url));
我知道他们在屏幕上制作了什么,但没有理解这些功能的关键部分。有人可以散发一点光。
//变量声明
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 DisplayMenu($buttons)
{
echo "<table width='100%' bgcolor='red' cellpadding='3'
cellspacing='4'\n";
echo " <tr>\n";
$width = 100/count($buttons);
while (list($name, $url) = each($buttons))
{
$this -> DisplayButton($width, $name, $url, !$this->IsURLCurrentPage($url));
}
echo " </tr>\n";
echo "</table>\n";
}
{
if ($active)
{
echo "<td width ='$width%'>
<a href ='$url'>
<img src ='s-logo' 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='test'>$name</span></td>";
}
}
public function IsURLCurrentPage($url)
//#determines weather a url for the button points to the current page.
// #the server[php_self,$url] returns a number if the string in $url is inside the superglobal variable $_server[pph]
{
if(strpos($_SERVER['PHP_SELF'], $url )==false)
{
return false;
}
else
{
return true;
}
}
public function DisplayButton($width, $name, $url, $active = true)
//outputs a single menu button if button is to point to the page to are on, you display an inactive button
#I think width just gives the table cell a % value. $name involves a setter function declared up top? ,
{
if ($active)
{
echo "<td width ='$width%'>
<a href ='$url'>
<img src ='s-logo' 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='test'>$name</span></td>";
}
}
答案 0 :(得分:0)
可能这会遗漏class
声明,但我们继续......
下面是声明类属性的位置。
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'
);
__set()
是PHP的神奇方法。您可以稍后检查所有这些here。我们将关注__set()
。这意味着每次在对象中设置属性时,它都会定义并设置此属性,即使您尚未在类中声明它。
// class Page's operations
public function __set($name, $value)
{
$this->$name = $value;
}
接下来,我们有DisplayMenu
,它会在屏幕上显示菜单。 $width
表示每个按钮的宽度。因此,如果$buttons
中有4个按钮,它将分割空间,在这种情况下为100%,因此,每个按钮占25%。
public function DisplayMenu($buttons)
{
echo "<table width='100%' bgcolor='red' cellpadding='3'
cellspacing='4'\n";
echo " <tr>\n";
$width = 100/count($buttons);
while (list($name, $url) = each($buttons))
{
// This will render the button on screen.
// Params:
// 1 The width of the button.
// 2 The name of the button.
// 3 The url the button points to.
// 4 Checks if the button url is the same url of the current page.
$this->DisplayButton($width, $name, $url, !$this->IsURLCurrentPage($url));
}
echo " </tr>\n";
echo "</table>\n";
}
IsURLCurrentPage
方法检查作为参数传递的$url
是否为当前URL并返回true或false。如果是当前页面则为True,否则为false。
public function IsURLCurrentPage($url)
{
if (strpos($_SERVER['PHP_SELF'], $url )==false)
{
return false;
}
else
{
return true;
}
}
最后,DisplayButton
将呈现实际按钮。它们中的每一个都是单独渲染的。
public function DisplayButton($width, $name, $url, $active = true)
{
// This will, in practice verify for you if
// the current page is the same of the button the user clicked.
// If it is, it will display the button without the link for that
// button, or with a disabled link.
// You'll notice it will show differently. Maybe
// also with some different color or style according to the CSS class.
// Remember that $active is the result of the IsURLCurrentPage().
if ($active)
{
echo "<td width ='$width%'>
<a href ='$url'>
<img src ='s-logo' 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='test'>$name</span></td>";
}
}