PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 8th, 2012, 04:36 PM
Bryan.ray Bryan.ray is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 6 Bryan.ray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 19 sec
Reputation Power: 0
PHP5 - Populating an array variable within a class

Hello group.

I have the following class....

PHP Code:
class Storm
{
  public 
$StormName;
  public 
$StormID;
  public 
$Latitude = array();  //used to hold a list of positions
  
public $Longitude = array(); //same as above



When I want to add values to my class I've been trying the following, but my arrays are not being populated.
I get an error message ... array_push first argument should be an array....

PHP Code:
 $aStorm = new Storm();
$aStorm->StormName 'test';  //works!
$aStorm->StormID '4';          //works! 
array_push($aStorm->Latitude'23.4'); //does not work
array_push($aStorm->Longitude'75.4'); //does not work 


Please help with the correct way to populate the array.
Many thanks!
Bryan

Reply With Quote
  #2  
Old October 8th, 2012, 04:43 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,476 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 5 h 48 m 16 sec
Reputation Power: 2194
Whoops, you gave the error, I scrolled past it.

It works for me:

php Code:
Original - php Code
  1. <?php
  2.  
  3. class Storm
  4. {
  5.   public $StormName;
  6.   public $StormID;
  7.   public $Latitude = array()//used to hold a list of positions
  8.   public $Longitude = array(); //same as above
  9. } 
  10.  
  11. $aStorm = new Storm();
  12. $aStorm->StormName = 'test'//works!
  13. $aStorm->StormID = '4';          //works!
  14. array_push($aStorm->Latitude, '23.4'); //does not work
  15. array_push($aStorm->Longitude, '75.4'); //does not work 
  16.  
  17. print_r($aStorm);


Result:
Code:
php -f storm.php 
Storm Object
(
    [StormName] => test
    [StormID] => 4
    [Latitude] => Array
        (
            [0] => 23.4
        )

    [Longitude] => Array
        (
            [0] => 75.4
        )

)


What version of PHP is this?
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

Reply With Quote
  #3  
Old October 8th, 2012, 05:08 PM
Bryan.ray Bryan.ray is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 6 Bryan.ray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by ptr2void
Whoops, you gave the error, I scrolled past it.

It works for me:

php Code:
Original - php Code
  1. <?php
  2.  
  3. class Storm
  4. {
  5.   public $StormName;
  6.   public $StormID;
  7.   public $Latitude = array()//used to hold a list of positions
  8.   public $Longitude = array(); //same as above
  9. } 
  10.  
  11. $aStorm = new Storm();
  12. $aStorm->StormName = 'test'//works!
  13. $aStorm->StormID = '4';          //works!
  14. array_push($aStorm->Latitude, '23.4'); //does not work
  15. array_push($aStorm->Longitude, '75.4'); //does not work 
  16.  
  17. print_r($aStorm);


Result:
Code:
php -f storm.php 
Storm Object
(
    [StormName] => test
    [StormID] => 4
    [Latitude] => Array
        (
            [0] => 23.4
        )

    [Longitude] => Array
        (
            [0] => 75.4
        )

)


What version of PHP is this?


I feel so stupid.... I misspelled a word (even though I spelled it correctly in the quick example above...) I've spent no less than 3 hours trying to figure this out and it comes down to something so simple....
Thanks for your help and clarity!
Regards,
Bryan

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP5 - Populating an array variable within a class

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap