object does not work within a single function

0

Good evening.

I have the following php structure

<?php
  require_once("testaAdmin.php");
  require_once("../_global/_erros/erros.ini");
  require_once("../_controlls/_util/Constantes.php");
  $constantes = new Constantes();

  if (isset($_POST["acao"]) && $_POST["acao"] == "cadastrar") {

  ......
function comparaArrays($array1, $array2) 
{
   if(is_array($array1) && count($array1) > 0) {
       $i = 0;
       foreach($array1 as $value1) {
           foreach($array2["name"] as $key =>$value2) {
               print $value2."<br>";
               if ($value1 == $value2) {
                   if ( $array2["error"][$key] == 0) {

                       $extensao = pathinfo($value2, PATHINFO_EXTENSION); 
                       $nomeFoto = md5(uniqid(time())).".". $extensao;

                       move_uploaded_file($array2["tmp_name"][$key], "../".$constantes->getEnderecoNormal()."/".$nomeFoto);

                       $fotosDao->cadastrar ($imoveisDao->ultimoIdCadastrado(), $nomeFoto);
                   }
                  $i++;
                  break;                                 
               }
           }
       }
   }
}

$arquivos1 = explode ("|", $_POST["arquivos"]);  
$arquivos2 = $_FILES["fotos"];

comparaArrays($arquivos1, $arquivos2);
.......
  }
 ?>

I have the $constantes->getEnderecoNormal() construct in the line of move_uploaded_file that php returns me that the object does not exist if placed inside the function. But if placed outside the function it works.

Where is the error?

Role change as amended:

///////////////////////////UPLOAD DAS FOTOS////////////////////////////////////
function comparaArrays($array1, $array2, $imoveisDao, $constantes, $fotosDao, $ultimoIdCadastrado) 
{
   if(is_array($array1) && count($array1) > 0) {
       $i = 0;
       foreach($array1 as $value1) {
           foreach($array2["name"] as $key =>$value2) {
               if ($value1 == $value2) {
                   if ( $array2["error"][$key] == 0) {

                       $extensao = pathinfo($value2, PATHINFO_EXTENSION); 
                       $nomeFoto = md5(uniqid(time())).".". $extensao;   

                       move_uploaded_file($array2["tmp_name"][$key], "../".$constantes->getEnderecoNormal()."/".$nomeFoto);

                       $fotosDao->cadastrar ($ultimoIdCadastrado, $nomeFoto);
                   }
                  $i++;
                  break;                                 
               }
           }
       }
   }
}

$arquivos1 = explode ("|", $_POST["arquivos"]);  
$arquivos2 = $_FILES["fotos"];

comparaArrays($arquivos1, $arquivos2, $imoveisDao, $constantes, $fotosDao, $imoveisDao->ultimoIdCadastrado());
////////////////////////////UPLOAD DAS FOTOS////////////////////////////////////
    
asked by anonymous 05.04.2016 / 03:27

1 answer

1

Well the point is that you are mixing object-oriented programming with structured (magic that "only PHP" allows and is hard-criticized, I particularly like it provided it is well done) but it does not work well, imagine that a PHP function is a 'cocoon' the data was not entered and the inside does not go out, one does not pass to the other in a 'normal' way so we can say, to understand the whole process will have to study OOP and Structured programming thoroughly to understand why all of this. So what I'm saying now is that to solve this NOW you have 3 solutions.

1) You pass your object through the function, ie the signature of your function:

<?php
$constantes = new Constantes();

function comparaArrays($cons, $array1, $array2) {
 // Código ...
 move_uploaded_file($array2["tmp_name"][$key], "../".$cons->getEnderecoNormal()."/".$nomeFoto);
 // Código ...
}

// Código ...
comparaArrays($constantes, $arquivos1, $arquivos2);
?>

2) Or you make your global variable throughout the function:

<?php
$constantes = new Constantes();

function comparaArrays($cons, $array1, $array2) {
 global $constantes;
 // Código ...
 move_uploaded_file($array2["tmp_name"][$key], "../".$constantes->getEnderecoNormal()."/".$nomeFoto);
 // Código ...
}

// Código ...
comparaArrays($arquivos1, $arquivos2);
?>

3) The third way is to start the class within the function itself:

<?php
function comparaArrays($array1, $array2) {
 $constantes = new Constantes();
 // Código ...
 move_uploaded_file($array2["tmp_name"][$key], "../".$constantes->getEnderecoNormal()."/".$nomeFoto);
 // Código ...
}
// Código ...
comparaArrays($arquivos1, $arquivos2);
?>
    
05.04.2016 / 03:47