What the?!
In real life, I'm a java programmer for hire, and in my secret life, I'm a beginning PHP enthusiast. I'm working on a piece of code that simply iterates through image files in a directory, creates objects out of them, and then stores them in an array. All very straightforward shit. The one piece of trickery that occurs is that I delegate the creation of the image objects to a factory. However, the factory is not returning the same object I had it create...when I examine the properties on the returned object, the data isn't there... The property values are empty strings, or more specifically, whatever the class had defined as the default value for that property. Furthermore, when I call an accessor method, it's returning me a weird and seemingly random number. I've attached the code and the output below. If anyone has any insight into what I'm doing wrong, please be so kind as to share it! Thanks!
# Code
getImage("$path/$file");
$fullpathmethod = $image->getFullPath();
$fullpathprop = $image->m_fullPath;
/* This line prints, but $fullpath is blank. */
echo "getFullpath() from getDir(): $fullpathmethod", "\r\n";
echo "fullpath property from getDir(): $fullpathprop", "\r\n";
$images[$i] =& $image;
$i++;
}
closedir($dir);
}
else
{
die("Not a valid filename");
}
}
class Image
{
var $m_fullPath = "nahnah";
var $m_fileName;
var $m_timestamp;
function getFullPath()
{
return $this->$m_fullPath;
}
function setFullPath($fullPath)
{
$this->$m_fullPath = $fullPath;
}
function getFileName()
{
return $this->$m_fileName;
}
function setFileName($fileName)
{
$this->$m_fileName = $fileName;
}
function getTimestamp()
{
return $this->$m_timestamp;
}
function setTimestamp($timestamp)
{
$this->$m_timestamp = $timestamp;
}
}
class ImageFactory
{
function & getImage($fullPath)
{
if (is_file($fullPath))
{
$newImage = new Image;
$baseFilename = basename($fullPath);
$timestamp = $this->parseTimestamp($baseFilename);
$newImage->setFullPath($fullPath);
/* This line prints just fine*/
echo "getFullPath from ImageFactory.getImage(): ", $newImage->getFullPath(), "\r\n";
$newImage->setFileName($baseFilename);
$newImage->setTimestamp($timestamp);
return $newImage;
}
else
{
die("File Not Found");
# return new NullImage();
}
}
function parseTimestamp($baseFilename)
{
$day = substr($baseFilename, 0, 2);
$month = substr($baseFilename, 2, 2);
$year = substr($baseFilename, 4, 2);
$hour = substr($baseFilename, 7, 2);
$minute = substr($baseFilename, 9, 2);
$second = substr($baseFilename, 11, 2);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
return $timestamp;
}
}
#Output
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195 608.jpg
getFullpath() from getDir(): 1030676168
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195 618.jpg
getFullpath() from getDir(): 1030676178
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195 628.jpg
getFullpath() from getDir(): 1030676188
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195 638.jpg
getFullpath() from getDir(): 1030676198
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195 649.jpg
getFullpath() from getDir(): 1030676209
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195 659.jpg
getFullpath() from getDir(): 1030676219
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195 719.jpg
getFullpath() from getDir(): 1030676239
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195 729.jpg
getFullpath() from getDir(): 1030676249
fullpath property from getDir(): nahnah
# Code
getImage("$path/$file");
$fullpathmethod = $image->getFullPath();
$fullpathprop = $image->m_fullPath;
/* This line prints, but $fullpath is blank. */
echo "getFullpath() from getDir(): $fullpathmethod", "\r\n";
echo "fullpath property from getDir(): $fullpathprop", "\r\n";
$images[$i] =& $image;
$i++;
}
closedir($dir);
}
else
{
die("Not a valid filename");
}
}
class Image
{
var $m_fullPath = "nahnah";
var $m_fileName;
var $m_timestamp;
function getFullPath()
{
return $this->$m_fullPath;
}
function setFullPath($fullPath)
{
$this->$m_fullPath = $fullPath;
}
function getFileName()
{
return $this->$m_fileName;
}
function setFileName($fileName)
{
$this->$m_fileName = $fileName;
}
function getTimestamp()
{
return $this->$m_timestamp;
}
function setTimestamp($timestamp)
{
$this->$m_timestamp = $timestamp;
}
}
class ImageFactory
{
function & getImage($fullPath)
{
if (is_file($fullPath))
{
$newImage = new Image;
$baseFilename = basename($fullPath);
$timestamp = $this->parseTimestamp($baseFilename);
$newImage->setFullPath($fullPath);
/* This line prints just fine*/
echo "getFullPath from ImageFactory.getImage(): ", $newImage->getFullPath(), "\r\n";
$newImage->setFileName($baseFilename);
$newImage->setTimestamp($timestamp);
return $newImage;
}
else
{
die("File Not Found");
# return new NullImage();
}
}
function parseTimestamp($baseFilename)
{
$day = substr($baseFilename, 0, 2);
$month = substr($baseFilename, 2, 2);
$year = substr($baseFilename, 4, 2);
$hour = substr($baseFilename, 7, 2);
$minute = substr($baseFilename, 9, 2);
$second = substr($baseFilename, 11, 2);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
return $timestamp;
}
}
#Output
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195
getFullpath() from getDir(): 1030676168
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195
getFullpath() from getDir(): 1030676178
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195
getFullpath() from getDir(): 1030676188
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195
getFullpath() from getDir(): 1030676198
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195
getFullpath() from getDir(): 1030676209
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195
getFullpath() from getDir(): 1030676219
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195
getFullpath() from getDir(): 1030676239
fullpath property from getDir(): nahnah
getFullPath from ImageFactory.getImage(): /home/joshdee/public_html/cam/290802@195
getFullpath() from getDir(): 1030676249
fullpath property from getDir(): nahnah
