I’m trying to incorporate the OOP principles with a web app I’m developing.
Given a class ImageData defined as
class ImageData {
private $count;
private $dirArray;
public function __construct() {
$count = 0;
$dir = "panos/thumbs";
$dirCon = opendir($dir);
while ($name = readdir($dirCon)){
if (strtolower(substr($name, -3)) == "jpg"){
$dirArray[]= $name;
}
}
sort($dirArray);
$count= count($dirArray);
closedir($dirCon);
}
public function getCount(){
return $this->count;
}
public function getArray(){
return $this->dirArray;
}
}
I want to use the information here to use in rendering a web page. Within the specified div element, I have
$imageData = new ImageData();
$count = $imageData->getCount();
$data = $imageData->getArray();
However, $count and $data are not instanciated locally, and the information contained is not available for usage in the render code. I’ve stepped through the code in xdebug, and the constructor assigns the expected values to $count and $data. In Java and C, its not required for variables to be instanciated at declaration. Is this true for PHP?
In your contructor, you have to use
$this->countinstead of$count, same for$dirArray.PS: You can in PHP classes only declare your variables as you did.