object does not work within single function

Asked

Viewed 56 times

0

good night.

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 building $constantes->getEnderecoNormal() on the line of move_uploaded_file php returns that the object does not exist if placed inside the function. But if placed outside the function it works.

Where is the error?

Change of function 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////////////////////////////////////

1 answer

1


Well the point is you’re mixing object-oriented programming with structured programming (magic that "only PHP" allows and is harshly criticized, I especially like as long as it is well done), in a way simutaneas side by side, but this way does not roll well, imagine that a function of PHP is a 'cocoon' the data of was does not enter and the inside does not come 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 in depth to understand why all of this. So what I say now is that to solve this NOW you have 3 solutions.

1) You pass your object by the function, ie in 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 overall variable in every 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);
?>
  • 3

    Several other languages besides PHP allow, such as C++, Harbour, and when well used, it’s great. The question problem is related to a programming error only. There is nothing wrong with using each paradigm where it is best. I agree with your "I particularly like it as long as it’s done right". The problem is that usually the staff has a very closed head to get out of the "reins" of the institution/course/influence where learned, so do not mix paradigm pq is "sin" kkk.

  • 1

    @Bacco put a few quotes on "only PHP" because we are talking about it in this topic, it was more a poetic license in the text than in fact only she do it, as for example I can mention the Javascript that makes this tbm mixture and I love it too. And I don’t think it’s wrong to use as long as I know what’s going on.

  • Well, I have added to the body of the question the new structure of the function and I would like to know your opinion on the amendment

  • Well the point is that you are mixing object-oriented programming with structured programming (magic that "only PHP" allows and is harshly criticized That’s news to me.

  • It’s because that function only fits that context and it’s just her in that situation. If I put it inside a class of utilities for example, it will not be able to be used because its development is very peculiar. But you did. Thank you!

  • Carlos, has your problem been solved? If so, define it as a better answer and avoid others wasting time trying to answer! As for the question of own opinions without foundations in studies, in the programming I advise to avoid to the maximum, seeing that the best is to know how to use each tool/ method in its hour.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.