Error: Call to a member function XXXX () on array

0

I have a class with a function that returns an array

function buscarPorComplejo($idComplejo){
  $sql = "SELECT * FROM XXX where id_complejo = '$idComplejo' ";
  $rs = mysql_query($sql) or die("Error en la Busqueda -cabanas-");
  $reg = 0;
  while($row = mysql_fetch_array($rs)){
    $vec["$reg"]["id"] = $row["id"];
    $vec["$reg"]["descripcion"] = $row["descripcion"];
    $vec["$reg"]["cantidadHuespedes"] = $row["cantidad_huespedes"];
    $vec["$reg"]["idComplejo"] = $row["id_complejo"];
    $vec["$reg"]["activo"] = $row["activo"];
    $reg++;
  }
  mysql_free_result($rs);
  if(isset($vec)){
    return $vec;
  }else{
    return false;
  }
}

This array I keep in a variable within an iteration.

foreach($arrayComplejo as $key=>$complejo){
    $arrayCabana = $cabana->buscarPorComplejo($complejo['id']);
}

On the second pass, I get the following error:

"PHP Fatal error: Call to a member function buscarPorComplejo() on array in ...."

I already tried cleaning the variable with unset but I can not avoid this error, any idea?

    
asked by Fakux 09.08.2016 в 23:10
source

2 answers

1

The problem was that inside the foreach:

foreach($arrayComplejo as $key=>$complejo){
    $arrayCabana = $cabana->buscarPorComplejo($complejo['id']);
}

The variable $ cabana that was used as the instance of the class was declared, change the name of the variable and it came out working normally.

    
answered by 10.08.2016 в 15:46
0

If it is already solved mark as correct any answer to close the question please. If you can not mark your own, I leave here the solution so that it can be closed.

Solved by the author of the question Fakux:

The problem was that inside the foreach:

foreach($arrayComplejo as $key=>$complejo){
    $arrayCabana = $cabana->buscarPorComplejo($complejo['id']);
}

The $ cabana variable that was used as the instance of the class was declared,

    
answered by 10.10.2016 в 11:44