我開始學習PHP。我寫了一個類,並在其中定義了一個數組。php類中的數組「Undefined variable」
<?php
/**
*
*/
class page {
public $content= "Coming soon";
public $title="Saeb Mollayi";
public $style ="<link rel=\"stylesheet\" type=\"text/css\" href=\"./style/style.css\">";
public $b = array(
"خانه" => 'homepage.php',
"ارتباط" => 'contact.php',
"خدمات" => 'services.php',
"نقشه سایت" =>'sitemap.php',
);
function displaymenu()
{
echo "<table>"."\r\n";
echo " <tr>";
$width=(100/count($b));
while (list($name,$url)= each ($b))
{
$this -> displaybuttons ($width,$name,$url, $this -> urlbool($url));
}
echo "</tr>";
echo "</table>";
}
function ncontent($newcontent)
{
$this-> content = $newcontent ;
}
function ntitle($newtitle)
{
$this -> title = $newtitle ;
}
function nbuttons($newbuttons)
{
$this -> b = $newbuttons ;
}
function display()
{
echo "<head>";
echo "\r\n";
$this -> displaytitle();
$this -> style ;
echo "</head>"."\r\n"."<body>"."\r\n";
$this -> displayheader();
$this -> displaymenu($this -> b);
echo $this -> content ;
$this -> displayfooter() ;
echo "</body>"."\r\n";
}
function displaytitle()
{
echo "<title>";
echo $this -> title ;
echo "</title>";
}
function displayheader()
{
echo '
<table id="header">
<tr id="header">
<td id="lheader"><img src="./img/logo1.png"></td>
<td id="cheader"> Welocme. Welcome! </td>
<rd id="rheader"><img src="./img/logo1.png"></td>
</tr>
</table>
';
}
function urlbool($url)
{
if (strpos($_SERVER['SCRIPT_NAME'],$url)==false)
return false;
else return true;
}
function displaybuttons($width,$name,$url,$active=false)
{
if (!active) {
echo "<td \" style =\"width=$width% ;\">
<a href ='$url'>
<img src=\".\img\top.png \" alt='$name' border='0'></a>
<a href='$url' ><span class='menu'>$name</span></a>
</td>";
} else {
echo "
<td style='width=$width%'>
<img src='./img/right.png'>
<span class='menu'>$name</span>
</td>";
}
}
function displayfooter()
{
echo "© footer";
}
}
?>
這是我的index.php文件:
<html>
<?php
require "./class/page0.inc";
$cc = array(
'خانه' => 'homepage.php',
'ارتباط' => 'contact.php',
'خدمات' => 'services.php',
'نقشه سایت' =>'sitemap.php',
);
$ppage = new page();
$ppage-> nbuttons(array(
'خانه' => 'homepage.php',
'ارتباط'=> 'contact.php',
'خدمات' => 'services.php',
'نقشه سایت' =>'sitemap.php',
));
$ppage -> ncontent('this is content'."<br>");
$ppage -> display();
?>
</html>
但是當我運行它,我看到這些錯誤:
注意:未定義的變量:b在 的/ opt/LAMPP /htdocs/all/test3/class/page0.inc on line 14
警告:在 /opt/lampp/htdocs/all /test3/class/page0.inc on line 14
警告:傳遞給每個()的變量不是數組或對象,位於第15行的 /opt/lampp/htdocs/all/test3/class/page0.inc
我在哪裏做錯了什麼?我的錯是什麼?
可能清理和縮進一些爲了可讀性的緣故? – Fallenreaper
您正在調用'$ this - > displaymenu($ this - > b);'帶有參數 - '$ this - > b',但函數聲明沒有param - >'函數displaymenu(){.. }'。您需要將其更改爲'function displaymenu($ b){...}' – Sean
「我的錯是什麼?」 - 如果第一次發生這種情況......不知道規則(用於訪問成員變量) - 未能閱讀文檔或書籍。如果這再次發生:懶惰。 –