Arrays of a class?

Hi everyone. Old to coding but new to here.

Im creating a class called ‘ownership’ which has a number of properties.

What I want to do is create an array of the class, and have a method called ‘getOwnerships’ that queries a MySQL database and returns a number of ownership records thus adding rows to an array of the class.

I’ve made a class that does the database query with its relevant methods and have:

public function getOwnerships() {
global $thisConnection;
$results=$thisConnection->processSql(my query**);
foreach($results as $result) {

  }

}

But now I’m stuck as to how to go about it. I’m sure its quite simple in reality. Any ideas please?

Thanks in advance,

Chris.

Going to need more information.

So,

public class Ownership  {

    private $_records = array();

    public function getOwnerships($thisConnection) {
        $results=$thisConnection->processSql(my query**);
        foreach($results as $result) {
            array_push($this->_records, $result);
        }
    }
}

Something like that then?

If you did your processSql properly you already have an array. You just need to return it.

Post the whole class.

Thanks for the reply. I do have an array but the names of the fields in the array came from the database which aren’t the same as the properties of the class.

Its an array of the class object I want.

Astonecipher:

Thanks for your response too, though what you create is a class containing an array, not an array of the class containing the data from the recordset.

Thanks again.

Chris

We will need to see your class.

I was guessing what you wanted based on what you said. If you just need an array of objects… Are you using PDO?
Return an array of objects. What the field names are is controlled by you. Use aliases for the field names so they match the class properties.

Sponsor our Newsletter | Privacy Policy | Terms of Service