DotDragnet
May 21, 2012, 04:37:15 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: php ArrayObject  (Read 1019 times)
Jeep Stone
Hero Member
*****
Posts: 908



View Profile WWW Awards
« on: October 23, 2007, 12:27:24 PM »

I've got a class called Gallery that returns an arrayobject populated with Images as follows:

Code:
class Gallery {

function __construct() {

}

function getImages() {
$count = 0;
$rootfolder = 'fpss';
$images = new ArrayObject();
$dir = $rootfolder . "/images";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($filename = readdir($dh)) !== false) {
if (($filename != ".") && ($filename != "..")) {
$imageURL = $dir . "/" . $filename;
$image = new Image($imageURL);
$images->append($image);

}
}
closedir($dh);
}
}
return $images;
}

}

class Image {

function __construct($imageURL) {
$this->imagePath = $imageURL;
}


}

I'm then trying to loop through these and write them out on the page:

Code:
$gallery = new Gallery();
$images = $gallery->getImages();

foreach ($images as $image) {
echo $image->imageURL;

However, this doesn't write anything out. Any ideas why? Do I need to access the ArrayObject differently?
Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 546



View Profile Awards
« Reply #1 on: October 23, 2007, 01:35:08 PM »

Quote
$this->imagePath = $imageURL;

BTW, what does ArrayObject get you that a plain old array doesn't?
Logged
Jeep Stone
Hero Member
*****
Posts: 908



View Profile WWW Awards
« Reply #2 on: October 23, 2007, 02:00:37 PM »

I wanted to return an array of Image objects so that if I needed to add extra properties to my image, I could access them nicely.
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!