Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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 answers
Answer question

0

Create an array and fill it.

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

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!