[英]How are these three functions working together?
I am following a php book and am not understanding a few functions. 我正在关注一本php书,并不了解一些功能。
Specifically,public function DisplayMenu($buttons). 具体来说,公共函数DisplayMenu($ buttons)。 I know this creates a table for the menu (home, contact etc and spaces them out).
我知道这会为菜单创建一个表格(主页,联系人等,并将它们分开)。 And the parameters here
和参数在这里
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));
I know what they produce on screen but not understadning the key parts of these fucntiosn. 我知道它们在屏幕上产生了什么,但没有完全理解这些功能的关键部分。 Can some one shed a tiny bit of light.
有人可以散发一点光。
// variables declared //声明变量
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>";
}
}
Probably this is missing the class
declaration, but we move on... 可能这是缺少
class
宣言,但我们继续......
Below, is where your class attributes are declared. 下面是声明类属性的位置。
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()
is a magic method from PHP. __set()
是PHP的神奇方法。 You can check all of them here later. 您可以稍后在此处查看所有这些内容。 We'll focus on
__set()
. 我们将专注于
__set()
。 It means that every time you set an attribute in your object it will define and set this attribute, even if you haven't declared it on your class. 这意味着每次在对象中设置属性时,即使您没有在类中声明属性,它也会定义和设置此属性。
// class Page's operations
public function __set($name, $value)
{
$this->$name = $value;
}
Next, we have DisplayMenu
, which renders the menu on screen. 接下来,我们有
DisplayMenu
,它在屏幕上呈现菜单。 The $width
, means the width of each button. $width
表示每个按钮的宽度。 So, if you have 4 buttons in $buttons
, it will split up the space, which in this case is 100%, among them, thus, resulting in 25% for each button. 因此,如果
$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
method checks if the $url
passed as argument is the current URL and returns true or false. IsURLCurrentPage
方法检查作为参数传递的$url
是否是当前URL并返回true或false。 True if it is the current page, false if not. 如果它是当前页面则为True,否则为false。
public function IsURLCurrentPage($url)
{
if (strpos($_SERVER['PHP_SELF'], $url )==false)
{
return false;
}
else
{
return true;
}
}
And finally, DisplayButton
will render the actual button. 最后,
DisplayButton
将呈现实际按钮。 Each one of them is rendered individually. 它们中的每一个都是单独渲染的。
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>";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.