Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a list or an array of objects in PHP?

Tags:

php

I'm a .net programmer vb & c#, but I can't seem to figure out how to get my objects into a list or array in PHP.

var mylist = new List<myobject>();

mylist.add(myobject1);
mylist.add(myobject2);

What I have tried.
Products being a property for a collection of orderitems:

$this->Products = getOrderItems();

public function getOrderItems()
{
    $items = array();
    $count = 0;

      // connect to db, query.....

    while($row = mysql_fetch_array($result, MYSQL_BOTH)){
        $count++;
        $items[$count] = ($row);
    }
    echo 'Count of Order Items...' . $count;

    return $items;
}

Am I even close?

like image 634
OneSmartGuy Avatar asked Jun 08 '09 02:06

OneSmartGuy


Video Answer


2 Answers

  • $this->Products = getOrderItems(); is legal in PHP, but it refers to the (global) function getOrderItems() instead of the class method. class methods and variables always have to be prefixed with $this-> (or self::, if they're static vars) when called from inside the class.
    in your sample-code, you have that wrong. getOrderItems is defined as class method, but your call is not $this->-scoped, thus php assumes a function. it should throw an function not found-error.

  • the [] notation adds an element to the end of an array.

  • the index of the first element in your sample code is 1 (isn't that the standard case for VB?). php normally starts at 0 - though it's possible (because php-arrays are not real arrays) to start at arbitrary indices i'd recommend to stick with zero.

  • mysql_fetch_array is an ancient way of working with mysql. nowadays you're better of with mysqli or (even better) PDO.

  • (...) a list or array in php.

    lists, arrays, stacks, whatever: in php everthing is an ordered map (misleadingly called array):

    PHP: Arrays: An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

update:

sorry, i haven't got enough time right now to explain the finer nuances of pdo/mysqli over mysql.

so here are just the basics:

  • oop: pdo and mysqli are object oriented (tough mysqli got functional aliases)

  • prep statements: most important: pdo/mysqli got prepared statements. that means, you first prepare the query with placeholders once, then fill in the values later (without the need to prepare the query a second time). this approach has 3 obvious advantages:

    • performance: it's faster, because the database only has to analyze, compile and optimize the query once (at least with complex queries)

    • security: no need for quoted strings (happens automatically!), making sql-injection attacks harder

    • maintainability: the logic and data part of the query are separated, thus easier to read and you don't have to do a lot of string concenation

  • driver driven: pdo is not database specific. there are several supported db-systems, making it easier to port your code to other db-backends (but it's not an db-abstraction layer like ODBC, so the SQL still has to be compatible) and increasing reusability

of course, there's a lot more to it

like image 113
stefs Avatar answered Sep 19 '22 21:09

stefs


$items = array();

while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    $items[] = $row;
}
echo 'Count of Order Items...', count($items);
like image 22
moo Avatar answered Sep 18 '22 21:09

moo