Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
List of object in PHP

I have a class called Person, with its getters and setters, and I want to know how I can return a List from Data Layer. In C# I use List and I can return the list, but in PHP I don't know how.

function AllPersons()
{
    try 
    {
        $objConn = new Connection();
        $conn = $objConn ->Connect();
        $sql = "SELECT * FROM PERSON";
        $answer= mysqli_query($cn, $sql);
        if(mysqli_num_rows($answer) > 0)
        {
            while($row = mysqli_fetch_array($answer)) 
            {
                /*here i want to do something like in C#
                  List<Person>listPerson;
                  listPerson.add(objPerson);*/
            }
        }
        else
        {
            return null;

        }
    } 
    catch (Exception $e) 
    {
        //FB::log("nada");
    }
}
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Create an array and fill it.

listPerson = [];   
while($row = mysqli_fetch_array($answer)) {
    listPerson[] = new Person($row);
}
about 4 years ago · Santiago Trujillo Relatório

0

In PHP arrays replace the use of Lists/Arrays you'd use in .NET. They're really flexible when it comes down to mutations.

In this case you'd probably approach it like:

...

$persons = array();
while($row = mysqli_fetch_array($answer)) 
{
    // Using [] appends the content of $row to the $persons array.
    $persons[] = $row;
}

...

Read more about the flexibility of PHPs arrays here.

about 4 years ago · Santiago Trujillo Relatório

0

List is a dimensionless array in C# (Can also be used in dimensional). The arrays in PHP also dimensionless. So you can use arrays.

...
$listPerson = array();
while($row = mysqli_fetch_array($answer)) 
            {
               $listPerson[] = objPerson;
            }
...
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda