I have a problem with PHP and I need your help to understand it, here is my code, create some attributes as an array to store information:
Pet.php
<?php
class mascota {
//Atributos
public $tipo = array();
public $nombre = array();
public $color = array();
public $raza = array();
public $edad = array();
// Métodos
public function mostrarMascota($nro,$tp,$nom,$col,$raz,$ed) {
array_push($tipo,$tip);
array_push($nombre,$nom);
array_push($color,$col);
array_push($raza, $raz);
array_push($edad,$ed);
$respuesta =
"
<table BORDER>
<tr>
<td>
$tipo[$nro];
</td>
<td>
$nombre[$nro];
</td>
<td>
$color[$nro];
</td>
<td>
$raza[$nro];
</td>
<td>
$edad[$nro];
</td>
</tr>
</table>";
return $respuesta:
}
}
?>
Index.php
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width-device-width, initial-scale-1.0">
<title>LABORATORIO NRO 3 - INGENIERÍA WEB</title>
</head>
<body>
<div class="container">
<h1>LABORATORIO NRO3 - INGENIERÍA WEB</h1>
<div class="creando-objeto">
<h2>Creación del objeto mascota</h2>
<table BORDER>
<tr>
<th>Mascota</th>
</tr>
<tr>
<td>Tipo</td>
</tr>
<tr>
<td>Nombre</td>
</tr>
<tr>
<td>Color</td>
</tr>
<tr>
<td>Raza</td>
</tr>
<tr>
<td>Edad</td>
</tr>
</table>
<?php
include('./Modelo/Mascota.php');
$objMascota = new mascota();
echo $objMascota->mostrarMascota(0,"PERRO","MILANESO","NEGRO",
"PASTOR ALEMAN",5);
?>
</div>
</div>
</body>
</html>
The result is this, I am just learning php and it seems to me that it is a problem of the arrays that I am doing wrong
I understand that you are starting. It's like @Benito-B says, it should be used
$this
to refer to all non-static members of a class.On the other hand, logic errors are noticed in your code. If the intention is to create elements in a specific position of each array, you must indicate it explicitly, when using
array_push()
the elements they will be inserted starting from the beginning, so if you pass a different value in the first parameter,0
the construction of the table will fail. Also,array_push
it is more expensive, and you should avoid it. Be that as it may, designing a class as you are doing is unfeasible, and is totally away from the purpose of the OOP, check the Post-Data where I will expand on this .Also, regarding
$respuesta
, you must end the line with a semicolon, and you can't use those calls as you have them, all inside a quote. For class members you should use{}
, for example:Either use
sprintf()
, I preferred the latter.Lastly, the naming convention is important. Don't use variables that no one knows what they mean, such as
$tp,$col,$raz,$ed
, within a month, you won't even know if$col
it refers to a column, a color... if$ed
it refers to age or eduardo, etc. If possible, use English names for your variables.I propose this code (to solve the errors you raise in the question):
And to test:
Departure:
Postscript
As I mentioned above, the current design of your class is unfeasible and misses the point of OOP.
You should think of classes that represent entities as templates to build one or more objects of that type . Your current class would handle an array of pets, if you really need that, you should build another class that collects them, and don't make the mistake of putting that logic inside the same class
Mascota
, because in some context, you will undoubtedly need to work with only one pet or present/map individualized data.Let's see an example, where I have created a class called
MascotaList
that manages an array of objects of typeMascota
.This would be the
Mascota
modified class. NOTE that the members of it are not arrays , but simple variables that represent each field of the entity. I have added some methods that will be useful to us later and I have created a constructor to pass the attributes when creating the instance of the class (then you can create setter too in case you need to modify any attribute after the instance is created).Mascota
Now let's see another class that will help us to collect pets.
MascotaList
If you notice, it
Mascota
's now a separate object, and you can create pet collections usingMascotaList
. Let's look at a contextual example:Departure:
But let's say you need data from a specific pet, say the first pet in the collection, and you want another format, not a table.
If you notice, the class
Mascota
has a methodprintData()
that was created for that purpose.Without modifying anything in the classes , you can satisfy this new requirement with this line of code:
Departure:
Or, suppose instead of a table you need to print the collection of pets as above, just add this method to
MascotaList
:Now, you will be able to do this from any context:
Departure:
In conclusion, your data model should be able to adapt to different contexts, being able to solve all the situations that arise with the same class: handling one or more pets, displaying the data in one way or another, etc.
To put a very frequent use case. Let's suppose that in one context you need to obtain data of a pet from a database table , and in another context from one
.json
exposed in an API and in another context from one.xml
offered by a Web Service. Well, the classMascota
that we have designed above will help you to map pet results in the three contexts . This is one of the great advantages of OOP. If you don't design with this in mind, you'll soon have spaghetti in your code and for the three use cases mentioned above you'll be creating three classes that ultimately do the same thing...show
Finally, I leave you with an online demo of the code implemented in the Post-Data, in case you want to do tests that help you better understand the OOP.
VER DEMO EN REXTESTER
I hope you find it useful. Any questions, you can ask in the comments area.
1- The
;
in is missingreturn $respuesta;
2- The 2nd argument of
MostrarMascota
is not$tp
, is$tip
3- You are defining attributes (
$tipo
,$nombre
,...), but inside the method you are trying to write local variables that do not exist. To be able to access these variables use the word$this->
4- You are using the wrong data structure for what you intend to do. Use an associative array
5- This is opinion based, but you can do the parameter assignment in the constructor
__construct()
, or else create a method to enter the data and another to display it. It doesn't make much sense to create a class that its only method receives a list of parameters and returns them as html.In this proposal, I modified your code with an attribute
data
, which accumulates the pets, and you can keep adding more:Another way, using a constructor to define each pet as an object, renamed the method
mostrarMascota
to simplymostrar
and no need for an accumulator array: