peuge Posted February 9, 2009 Share Posted February 9, 2009 Hey all, I have a class, and some of the methods are going to need to return arrays, eg. Query a database and return an array of names. So to begin I tried something really simple just to see how this would work (I am new to OOP) //in another php page $amnt = new test_class; $arr = $amnt->test_array(); for ($i=1;$i<10;$i++){ echo $arr[$i]; } //in the class var $arr = array(); function test_array(){ while ($this-> i != 10){ $this->$arr[$this->i]= $this->i; //This is line 40 $this->i++; } return $this->arr; } from This i get this error Fatal error: Cannot access empty property in C:\xampp\htdocs\zaart\gal_class.php on line 40 Can anyone please help me, much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/144461-solved-returning-array-in-class/ Share on other sites More sharing options...
RichardRotterdam Posted February 9, 2009 Share Posted February 9, 2009 can you show the class code? Quote Link to comment https://forums.phpfreaks.com/topic/144461-solved-returning-array-in-class/#findComment-758028 Share on other sites More sharing options...
peuge Posted February 9, 2009 Author Share Posted February 9, 2009 <?php class file_handle{ var $name; private $dir_images = "images/"; private $dir_big = "big/"; var $i=0; var $first_pic_title; var $arr = array(); //function __construct($names){ // $this->name = $names; //} function amount_pics(){ if ($handle = opendir($this->dir_images)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != rtrim($this->dir_big,"/")) { $this->i++; // = $this->i + 1; } } closedir($handle); } else{ $this->i = 0; } return $this->i; } function get_first_pic($artist,$cat){ $sql = "SELECT * FROM pictures WHERE artist = '$artist' AND cat = '$cat'"; $result = mysql_query($sql); $row=mysql_fetch_array($result); $this->first_pic_title = $row["title"]; return $this->first_pic_title; } function test_array(){ while ($this-> i != 10){ $this->$arr[$this->i]= $this->i; $this->i++; } return $this->arr; } } Sorry bout the indentation, this editor doesnt take it exactly how I have it. Quote Link to comment https://forums.phpfreaks.com/topic/144461-solved-returning-array-in-class/#findComment-758030 Share on other sites More sharing options...
trq Posted February 9, 2009 Share Posted February 9, 2009 For testing purposes I would replace this.... function test_array(){ while ($this-> i != 10){ $this->$arr[$this->i]= $this->i; $this->i++; } return $this->arr; } with.... function test_array() { for (range(0,10) as $count) { $this->$arr[$count] = $count; } return $this->arr; } ps: Your also using the older php4 class syntax. Quote Link to comment https://forums.phpfreaks.com/topic/144461-solved-returning-array-in-class/#findComment-758033 Share on other sites More sharing options...
peuge Posted February 9, 2009 Author Share Posted February 9, 2009 Cool thanks. Been away from php for a while now, so gotta get with the new stuff. I am now getting this error? Parse error: syntax error, unexpected T_AS, expecting ';' on this line for (range(0,10) as $count) { Quote Link to comment https://forums.phpfreaks.com/topic/144461-solved-returning-array-in-class/#findComment-758041 Share on other sites More sharing options...
trq Posted February 9, 2009 Share Posted February 9, 2009 Sorry, typo. Should be foreach not for. Quote Link to comment https://forums.phpfreaks.com/topic/144461-solved-returning-array-in-class/#findComment-758044 Share on other sites More sharing options...
peuge Posted February 9, 2009 Author Share Posted February 9, 2009 No problem, thanks. But now still getting the Fatal error: Cannot access empty property in C:\xampp\htdocs\zaart\gal_class.php on line 41 on this line: $this->$arr[$count] = $count; Quote Link to comment https://forums.phpfreaks.com/topic/144461-solved-returning-array-in-class/#findComment-758049 Share on other sites More sharing options...
trq Posted February 9, 2009 Share Posted February 9, 2009 Doh, another typo. Having a shocker. $this->arr[$count] = $count; Quote Link to comment https://forums.phpfreaks.com/topic/144461-solved-returning-array-in-class/#findComment-758056 Share on other sites More sharing options...
peuge Posted February 9, 2009 Author Share Posted February 9, 2009 lol also didnt see that thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/144461-solved-returning-array-in-class/#findComment-758061 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.