Reputation: 1
This is homework. I must say that the instructions are not very clear, but this is what I've "deciphered". I must create 1 class with 1 "subclass" in different files. Parent class is:
<?php
class Objecte
{
var $model;
private $preu;
public function __construct($model,$preu)
{
$this->model=$model;
$this->preu=$preu;
}
}
?>
The "subclass" is:
<?php
include('classe_objecte.php');
class Ordinador extends Objecte
{
private $disc_dur;
private $ram;
public function Ordinador($model,$preu,$disc_dur,$ram)
{
parent::__construct($model,$preu);
$this->disc_dur=$disc_dur;
$this->ram=$ram;
}
}
?>
I must create an "ordinador" object with properties $model,$preu,$disc_dur,$ram to show them later. This information comes from an HTML form that sends it via $_POST to a file called "ordinadors.php". As I'll have more than 1 object, I want the object name to be a variable variable. The name will be the word "ordinador" and a number provided in a session variable called 'numOrdinadorsO'. This is part of the "ordinadors.php" code:
include_once('classe_ordinador.php');
/*some other code here*/
$name="ordinador".$_SESSION['numOrdinadorsO'];
$$name=new Ordinador($model,$preu,$disc_dur,$ram);
If I var_dump($$name), I get this:
object(Ordinador)#1 (4) { ["disc_dur":"Ordinador":private]=> string(3) "100" ["ram":"Ordinador":private]=> string(1) "8" ["model"]=> string(7) "modelo1" ["preu":"Objecte":private]=> string(3) "200" }
After creating the object, I must search for an specific "model" and return the other attributes related to that model (preu, ram, etc.). And there problems start. This is the code:
function consultarOrdinadorO()
{
$model_buscat=$_POST['ordinador_busqueda'];
$valor=$_SESSION['numOrdinadorsO'];
$contador=0;
$trobat=false;
do
{
for ($i=0; $i<$valor; $i=$i+1)
{
$name="ordinador".$i;
if ($$name->model==$model_buscat) <-- this is line 91
{
echo "Model $model_buscat trobat.";
$trobat=true;
break;
} else {
$contador+=1;
}
}
}
while ($trobat==false && $contador<$valor);
if ($trobat==false)
{
echo "Model $model_buscat no trobat.";
}
}
I get this error (I have insered information of 3 "ordinadors" before):
Notice: Undefined variable: ordinador0 in C:\Users\Quenya\IOC\2014-2015\S2\M09 PHP\EAC1\Ejercicios\Exercici2\ordinadors.php on line 91
Notice: Trying to get property of non-object in C:\Users\Quenya\IOC\2014-2015\S2\M09 PHP\EAC1\Ejercicios\Exercici2\ordinadors.php on line 91
Notice: Undefined variable: ordinador1 in C:\Users\Quenya\IOC\2014-2015\S2\M09 PHP\EAC1\Ejercicios\Exercici2\ordinadors.php on line 91
Notice: Trying to get property of non-object in C:\Users\Quenya\IOC\2014-2015\S2\M09 PHP\EAC1\Ejercicios\Exercici2\ordinadors.php on line 91
Notice: Undefined variable: ordinador2 in C:\Users\Quenya\IOC\2014-2015\S2\M09 PHP\EAC1\Ejercicios\Exercici2\ordinadors.php on line 91
Notice: Trying to get property of non-object in C:\Users\Quenya\IOC\2014-2015\S2\M09 PHP\EAC1\Ejercicios\Exercici2\ordinadors.php on line 91
Any help?. I've looked in the forums for other questions like this but I haven't found any. I guess I'm probably not understanding well how objects and classes work, but the information provided by the teacher is... very limited.
Thanks a lot!.
UPDATE 1: Thanks u_mulder, I think I'm seeing some light now. But... I keep having some problems :-(.
Additional information I missed before: I have an HTML form with 2 buttons. - Button "inserir": I insert the ordinador information. Button "consultar": I insert a model in a text box, and I click the model so it searches the model.
Code in ordinadors.php to specify which funtion call when I click one button or the other:
session_start();
include_once('classe_ordinador.php');
$objetos=arrray();
//some other code here
if (isset($_POST['inserir']))
{
inserirOrdinador();
inserirOrdinadorS();
inserirOrdinadorO();
var_dump($objetos);
} elseif (isset($_GET['consultar'])=='consultar')
{
consultarOrdinador();
} elseif (isset($_POST['consultar_ordinador']))
{
if (empty($_POST['ordinador_busqueda']))
{
echo "Model no introduit.";
}else
{
var_dump($objetos);
/*
consultarOrdinadorS();
consultarOrdinadorO($objetos);
*/
}
}
inserirOrdinadorO now looks like this:
function inserirOrdinadorO()
{
$_SESSION['ordinadorsO']=array('model_ordinadors'=>$_POST['model_ordinadors'],'preu_ordinadors'=>$_POST['preu_ordinadors'],'tamany'=>$_POST['tamany'],'ram'=>$_POST['ram']);
$model=$_SESSION['ordinadorsO']['model_ordinadors'];
$preu=$_SESSION['ordinadorsO']['preu_ordinadors'];
$disc_dur=$_SESSION['ordinadorsO']['tamany'];
$ram=$_SESSION['ordinadorsO']['ram'];
global $objetos;
$obj_id = $_SESSION['numOrdinadorsO'];
$ord_tmp = new Ordinador($model,$preu,$disc_dur,$ram);
$objetos[$obj_id] = $ord_tmp;
$_SESSION['numOrdinadorsO']+=1;
echo "Objecte Ordinador inserit.</br>";
}
So the var_dump($objetos) here is showing me the values I added in inserirOrdinadorO:
if (isset($_POST['inserir']))
{
inserirOrdinador();
inserirOrdinadorS();
inserirOrdinadorO();
var_dump($objetos);
but the one here
} elseif (isset($_POST['consultar_ordinador']))
{
if (empty($_POST['ordinador_busqueda']))
{
echo "Model no introduit.";
}else
{
var_dump($objetos);
returns this:
array(0) { }
I guess that it's because it's reading $objetos here and of course it's empty:
session_start();
include_once('classe_ordinador.php');
$objetos=array();
But... how do I make it so I can read it from there too?.
Sorry, I'm just begining with php. I read the link you sent but I think I'm missing the point here :-(
Thanks a lot for your help.
Upvotes: 0
Views: 54
Reputation: 54841
First, when you have a bunch of similar objects and you need to store them - use array. So, when you have some session value:
$obj_id = $_SESSION['numOrdinadorsO'];
// instead of using variable variables use arrays:
$objects = array(); // array to store your objects
$ord_tmp = new Ordinador($model,$preu,$disc_dur,$ram);
// Now you have all your objects of type Ordinador in an array
$objects[$obj_id] = $ord_tmp;
Your main problem is function scope. When you call for consultarOrdinadorO
, this function works in it's own scope, so it doesn't know about any outer variables.
There are two ways of solving this:
In your current function, before you try to access $$name
you should declare it as global
, so that function know that this variable is defined somewhere else:
for ($i=0; $i<$valor; $i=$i+1)
{
$name="ordinador".$i;
// add the following line
global $$name;
// other code is the same
Or you can pass your variables as arguments. But in your case you don't know how many objects do you have. That's why I advise you to use arrays. After you get all Ordinators to array you can define your function as:
function consultarOrdinadorO($ordinators)
{
// code skipped
for ($i=0; $i<$valor; $i=$i+1)
{
$cur_obj = $ordinators[$i];
if ($cur_obj->model == )
// code skipped
}
Assuming you collect all your objects earlier to $objects
array, function call will be consultarOrdinadorO($objects)
More about functions here: https://www.php.net/language.functions.php
Upvotes: 1