【问题标题】:Problem with the call to method inside the class [closed]类内调用方法的问题[关闭]
【发布时间】:2020-10-10 17:25:55
【问题描述】:

我正在学习 PHP,在本课中,我正在学习在类中调用方法。我不明白为什么在我的表格单元格中显示数字的方法没有显示任何内容。我复习了很多次,但我能找到问题所在。可以吗?

<?php 

class Tabla {

    private $row;
    private $column;
    private $mat = array();
    
    public function __construct ($fila, $columna) {
        $this -> row = $fila;
        $this -> column = $columna;
    }




    public function cargar ($fil, $col, $tex) {
        $this -> mat[$fil][$col] = $tex;
    }




    public function mostrar ($fil, $col) {
        echo '<td width="50" height="25">'. $this -> mat[$fil][$col] .'</td>';
    }


    public function graficar () {
        echo '<table border="1">';
        
            echo '<tbody>';
                for($f = 1; $f <= $this -> row; $f++) {
                    echo "<tr>";
                    for($i = 1; $i <= $this -> column; $i++) {
                        $this -> mostrar($fil, $col);
                    }
                    echo "</tr>";
                }
            echo '</tbody>';
        echo '</table>';
    }

}

$tablita = new Tabla (3, 3);
$tablita -> cargar(1, 1, "1");
$tablita -> cargar(1, 2, "2");
$tablita -> cargar(1, 3, "3");
$tablita -> cargar(2, 1, "4");
$tablita -> cargar(2, 2, "5");
$tablita -> cargar(2, 3, "6");
$tablita -> cargar(3, 1, "7");
$tablita -> cargar(3, 2, "8");
$tablita -> cargar(3, 3, "9");
$tablita -> graficar ();

【问题讨论】:

  • Las preguntas y respuestas aquí deben estar escritas en inglés。赞成利用 es.stackoverflow.com。 (这里的问题和答案必须用英文写,请使用es.stackoverflow.com/.
  • 您的脚本打印出a lot of stuff。你能更具体一点吗?您可能还想修复缺失的变量。
  • 当然...@ÁlvaroGonzález 我的脚本打印一个表格,该表格的单元格应该是我在“Cargar”方法中引入的数字,但这些数字没有显示在单元格中.表格打印的html确实不错,但是没有出现数字。我查看了代码,我认为问题出在对“mostrar”方法的调用中,但我看不出有什么不同或不好。你能看到吗?
  • 但是您正在读取甚至不存在的变量。这不可能。

标签: php class methods


【解决方案1】:

我练习中的问题在于方法“graficar”,当它使用“mostrar”方法时......因为我设置了不存在的变量,所以在这种情况下我们应该使用循环的变量,表示表格的行和列,这样:

public function graficar () {
        echo '<table border="1">';
        
            echo '<tbody>';
                for($f = 1; $f <= $this -> row; $f++) {
                    echo "<tr>";
                    for($i = 1; $i <= $this -> column; $i++) {
                        $this -> mostrar($f, $i);
                    }
                    echo "</tr>";
                }
            echo '</tbody>';
        echo '</table>';
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-09
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多