I'll tell you some things.
A. Regarding the error you have in question
In PHP the functions can receive arguments (simple variables, fixes, etc):
Function arguments
Any information can be passed to the functions through the
list of arguments, which is a list of delimited expressions
by commas. The arguments are evaluated from left to right.
In your case, the function needs to know the argument with which it will work. If it is not declared within the function and it is not a variable of the global type, then you have to pass it to it when you call the function:
Function:
function mostrarMatriz($m)
{
for ($i = 0; $i < $m->getFilas(); $i++)
{
//...
}
}
Calling the function:
$m = new Matriz();
mostrarMatriz($m);
With this your problem would be solved.
B. But ...
It is possible to do it in another way. Seen the code, one wonders why not create the function mostrarMatriz
within Clase Matriz()
?
It would not be a bad practice to create a class inside a getter of the complete matrix, in fact, this is what the OOP recommends (when we work with classes). In this way your Matrix class would have different ways of obtaining the data. Just as you have a method getFilas()
, getColumnas()
, you could have a method getMatriz()
that would return the matrix.
For this the function should have a return.
EDIT:
Possible Model of a Parent Class
class Matriz
{
private $filas;
private $columnas;
/*
*Constructor con parámetros los cuales deben ser pasados
* cuando se crea una instancia de la clase
* usando por ejemplo: $m = new Matriz ("10", "60");
* nótese que el tipo de dato puede ser otro que String, indicándolo
* debidamente a la clase
*/
public function __construct($filas, $columnas)
{
$this->filas = $filas;
$this->columnas = $columnas;
//... todas las demás variables que formarán parte de la clase y que se reciben en parámetro
// ... las demás, ejemplo las que devuelven los métodos públicos no necesitan declararse
// ... aquí sólo las variables que la clase necesita *conocer* para trabajar con ellas
}
//En este caso el método no necesita parámetros
public function getMatriz()
{
for ($i = 0; $i < $this->filas; $i++)
{
//... aquí se crea una representación de la matriz en una variable o lo que se quiera
// es lo que se enviará en el return
}
return $variableconcatenadaenelbuclefor
}
public function getFilas()
{
return $this->filas;
}
public function getColumnas()
{
return $this->columnas;
}
}
As you see, in the class the other methods that the constructor do not receive any parameter, and we can access the rows / columns received in the constructor using the pseudo variable $this
.
Example of use:
//Se pasan los valores al contructor, en este caso los valores filas y columnas
$m = new Matriz ("10", "50");
echo "Filas: ". $m->getFilas();
echo "Columnas: ". $m->getColumnas();
echo "Matriz: ". $m->getMatriz();
With this implementation the code would be easier to maintain and if you have to repeat the matrix sample from several files, you would do it with two lines of code, otherwise, in each place where you need to show it, you would have to repeat the function that opens the loop for.