SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Hybrid View

  1. #1
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    (help) Creating an Array of Objects

    How do I create an array of objects? I'm quite new to OOP, please bear with me.

    I have a car rental database (mysql) that retrieves all availiable cars. In each result row, it has: make, model, trans, doors, color, etc.

    The Car class has getters and setters for these attributes. Before displaying all available cars, it will save each individual car into $myCar[]. However, whenever the script runs, i get this error:
    Code:
    small text
    Code:
    Warning: Cannot use a scalar value as an array in /hsphere/local/home/cis235/reservation/index.php on line 46
     
    Fatal error: Call to a member function on a non-object in /hsphere/local/home/cis235/reservation/index.php on line 46


    Here is my code snippet

    Code:
    small text here
    Code:
    function showCars()
    {
    require('includes/car_class.inc.php');
    $myCar = new Car();
     
    # Get list of available cars
    $SQL = 'SELECT DISTINCT 
    	`car_description`.`car_make`,
    	`car_description`.`car_model`,
    	`car_description`.`car_trans`,
    	`car_description`.`car_doors`,
    	`car_description`.`car_color`,
    	`car_description`.`car_type`,
    	`car_description`.`car_year`,
    	`car`.`car_vin`
    FROM
    	`car`
    	INNER JOIN `car_linker` ON (`car`.`car_vin` = `car_linker`.`car_vin`)
    	INNER JOIN `car_description` ON (`car_linker`.`car_id` = `car_description`.`car_id`)
    WHERE
    	(`car`.`available` = 1)
    ORDER BY
    	`car_description`.`car_make`';
     
    $result = mysql_query($SQL);
     
    while ($rowInfo = mysql_fetch_assoc($result))
    { 
    	 $myCar[]->setMake($rowInfo['car_make']);
    	 $myCar[]->setModel($rowInfo['car_model']);
    	 $myCar[]->setTrans($rowInfo['car_trans']);
    	 $myCar[]->setDoors($rowInfo['car_doors']);
    	 $myCar[]->setColor($rowInfo['car_color']);
    	 $myCar[]->setType($rowInfo['car_type']);
    	 $myCar[]->setYear($rowInfo['car_year']);
    	 $myCar[]->setVin($rowInfo['car_vin']); 
    }
    }


    AHHH!!! Finally. after 34 trys, vB 3.0b isn't adding font tags in my php snippet. Nevermind...only a few font tags...good enough. How do you disable the V_BULLETIN_BS_TAGS?

    NO WORKING EDIT AND NO PHP TAGS MAKE GEEKSUPPORT GO SOMETHING SOMETHING....

    Sorry for not using the php vb tags but, i can't figure out how to increase the text w/o adding all those font tags.
    Last edited by GeekSupport; Mar 6, 2003 at 03:01.

  2. #2
    SitePoint Member
    Join Date
    Mar 2003
    Location
    Bergen, Norway
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to create an instance of Car for each row returned from your query, like this:
    Code:
    $allCars=array();
    // sql query deleted..
    $result = mysql_query($SQL);
     
    while ($rowInfo = mysql_fetch_assoc($result))
    { 
    	 $tempCar=new Car();
     
    	 $tempCar->setMake($rowInfo['car_make']);
    	 $tempCar->setModel($rowInfo['car_model']);
    	 $tempCar->setTrans($rowInfo['car_trans']);
    	 $tempCar->setDoors($rowInfo['car_doors']);
    	 $tempCar->setColor($rowInfo['car_color']);
    	 $tempCar->setType($rowInfo['car_type']);
    	 $tempCar->setYear($rowInfo['car_year']);
    	 $tempCar->setVin($rowInfo['car_vin']); 
    	 $allCars[]=$tempCar;
    }


    This way, you get an array constisting of instances of Car. In your code, you created myCar as an instance of Car. When you later use $myCar[] you will get an error, since myCar is an object, not an array..

    hope I made clear..

    Jo

  3. #3
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you specify $myCar as an Object, yet you try to use it as if its an array. So it sounds to me like you need some kind of internal array which you add new elements to via a set method of some kind.

    Like $myCar->addVar($myCar->setVin($rowInfo['car_vin'])

  4. #4
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your example made it very clear.

    Thanks johavik and Phil.

    quick question. i was searching google and saw some people creating new objects like this

    $personObj = &new Person('andrew', '22', 'male');

    Whats the difference between &new and new?
    Last edited by GeekSupport; Mar 6, 2003 at 04:01.

  5. #5
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    &func() can return a reference whereas func() can only return a value.

    well, in procedural work it does.
    teckis - that's news to me.

  6. #6
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using & returns a reference..

    If you don't return a reference, then you are actually creating 2 objects, a temporary object, and the one that is returned to you...

    There for:

    $object = new Object(); = $object, and a temp object stored in memory

    $object =& new Object(); = $object being a memory reference, which would decrease your overhead.
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  7. #7
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there any benefit creating a new object without =& ?

  8. #8
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GeekSupport
    is there any benefit creating a new object without =& ?
    Well, it's an easy way to create a copy of an object which can come in handy, albeit seldomly. 99% of the time you want a reference not a copy, so always use the good ol' ampersand.

  9. #9
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there a special way of accessing the copy?

    $obj1 = new Object(); = $obj1, and a temp object stored in memory

    how would i access $obj1's copy?

  10. #10
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GeekSupport
    is there a special way of accessing the copy?
    Nope, just do a regular assignment:

    PHP Code:
    <?php
     
    $obj1 
    =& new Object();
    $obj2 $obj1;
     
    ?>
    Now, if you change $obj1, $obj2 will not change.

  11. #11
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what if you didn't use the =&? Zaire mentioned that using $obj = new Person(); would create your $obj and a temporary object in memory. My understanding is that when you do $obj = new Person(); 2 objects are created -- $obj and a memory resident object.

    how would you use the memory resident object than $obj w/o making a copy via $obj2 = $obj;

  12. #12
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, now I understand what you're asking.

    When you do:

    <?php $obj = new Object(); ?>

    two things happen. The 'new Object();' part results in an object being created in memory. The '$obj =' part results in setting $obj equal to a copy of the object that was just created. After that statement executes, the memory-resident object is inaccessible to you which is why it's best to always use the ampersand:

    <?php $obj =& new Object(); ?>

    That way, $obj is set equal to the object just created, and there's no extra object floating around in memory.

  13. #13
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clarification codezilla

  14. #14
    SitePoint Member alfmarius's Avatar
    Join Date
    Nov 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Update

    I came across this thread, and would just like to make an more updated comment.

    http://no.php.net/manual/en/language...ces.whatdo.php states:

    "Since PHP 5, new return reference automatically so using =& in this context is deprecated and produces E_STRICT level message."

    alf

  15. #15
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And... You reincarnated a dead thread from three years ago, to basically tell everyone something we all knew already?

    Anyways, welcome to Sitepoint; You've made a grand entrance, that's all

  16. #16
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thought I'd reincarnate this thread yet 3 years later to say thanks alfmarius for the fyi. Still useful and 2nd google result right after php dot net.

  17. #17
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Well, seeing as its been resurrected, what about using clone instead?

    I found this tutorial which returns an array of objects which are clones of an object.

    Anyone have a point of view about the use of clone vs using &= ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •