【问题标题】:array in php class "Undefined variable"php类“未定义变量”中的数组
【发布时间】:2016-05-22 18:12:59
【问题描述】:

我开始学习 PHP。我写了一个类,并在其中定义了一个数组。

<?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 in /opt/lampp/htdocs/all/test3/class/page0.inc 在第 14 行

警告:除以零 /opt/lampp/htdocs/all/test3/class/page0.inc 在第 14 行

警告:传递给 each() 的变量不是数组或对象 /opt/lampp/htdocs/all/test3/class/page0.inc 在第 15 行

我在哪里做错了什么?我的错是什么?

【问题讨论】:

  • 为了可读性可以清理和缩进一些吗?
  • 您正在使用参数调用$this -&gt; displaymenu($this -&gt; b); - $this -&gt; b,但您的函数声明没有参数 -> function displaymenu(){...}。您需要将其更改为function displaymenu( $b ){...}
  • “我的错是什么?” - 如果这是第一次发生......不知道规则(用于访问成员变量) - 没有阅读文档或书籍。如果这种情况再次发生:懒惰。
  • @KarolyHorvath 成就了我的一天。
  • :我用我的英语和 php 工作

标签: php arrays class


【解决方案1】:

人类可读的错误文本,它们的意思是:

  1. page.inc 文件第 14 行的函数中不存在变量 $b。
  2. 因为 $b 不存在,PHP 函数 "count" 将返回 0 并且你被零除。
  3. 您将变量 $b 传递给函数“each”,$b 不存在且它不是数组或对象。

如果你在类中有一个变量 $b,使用 $this->b 来访问它。

【讨论】:

    【解决方案2】:

    我不知道是不是因为在复制/粘贴中出了点问题,但是编辑你的帖子我发现有很多语法错误。

    首先是对象函数调用中的那些空格,其中有不必要的空格。

    箭头不能像$this -&gt; displaymenu($this -&gt; b);那样有空格,必须是$this-&gt;displaymenu($this-&gt;b);

    此外,$b 必须使用 $this-&gt;b 引用,这就是为什么会出现除以零的错误。

    【讨论】:

    • tnx 。我必须在每个我有 $b 功能的地方使用 $this->b。 tnx
    • 不客气!如果这是您的解决方案,如果您选择它作为您的答案,那就太好了。
    猜你喜欢
    • 2022-08-03
    • 2012-03-04
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 2016-09-22
    相关资源
    最近更新 更多