2016-02-11 38 views
0

我開始學習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 "&copy 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

我在哪裏做錯了什麼?我的錯是什麼?

+1

可能清理和縮進一些爲了可讀性的緣故? – Fallenreaper

+0

您正在調用'$ this - > displaymenu($ this - > b);'帶有參數 - '$ this - > b',但函數聲明沒有param - >'函數displaymenu(){.. }'。您需要將其更改爲'function displaymenu($ b){...}' – Sean

+1

「我的錯是什麼?」 - 如果第一次發生這種情況......不知道規則(用於訪問成員變量) - 未能閱讀文檔或書籍。如果這再次發生:懶惰。 –

回答

1

我不知道是不是因爲在複製/粘貼的時候出錯了,但是編輯你的帖子後我發現有很多語法錯誤。

首先在對象函數調用中的那些空間中,您有不必要的空間。

箭頭不應該有任何空間像$this -> displaymenu($this -> b);,它必須是$this->displaymenu($this->b);

此外$b必須使用$this->b引用,這就是爲什麼你得到有關除以零的錯誤。

+0

tnx。我必須使用$ this-> b,每一處我都有$ b的函數。 tnx –

+0

不客氣!如果這是你的解決方案,如果你選擇它作爲你的答案,那將是善意的。 – phaberest

1

文本中讀取錯誤人類,他們的意思是:

  1. 變量$ B不會在你的函數存在於page.inc文件第14行。
  2. 因爲$ b不存在,所以PHP函數「count」將返回0,並且您除以零。
  3. 您將變量$ b傳遞給函數「each」,$ b不存在,它不是數組或對象。

如果在類內有變量$ b,請使用$ this-> b來訪問它。

相關問題