Guest User

Untitled

a guest
Sep 12th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. Incorporating OOP in existing PHP
  2. class ImageData {
  3. private $count;
  4. private $dirArray;
  5.  
  6. public function __construct() {
  7. $count = 0;
  8. $dir = "panos/thumbs";
  9. $dirCon = opendir($dir);
  10.  
  11. while ($name = readdir($dirCon)){
  12. if (strtolower(substr($name, -3)) == "jpg"){
  13. $dirArray[]= $name;
  14. }
  15. }
  16.  
  17. sort($dirArray);
  18. $count= count($dirArray);
  19.  
  20. closedir($dirCon);
  21. }
  22.  
  23. public function getCount(){
  24. return $this->count;
  25. }
  26.  
  27. public function getArray(){
  28. return $this->dirArray;
  29. }
  30.  
  31. }
  32.  
  33. $imageData = new ImageData();
  34. $count = $imageData->getCount();
  35. $data = $imageData->getArray();
  36.  
  37. class ImageData {
  38. protected $count = 0;
  39. protected $dir = null;
  40. protected $dirArray = null;
  41.  
  42. public function __construct( $dir = 'panos/thumbs') {
  43. $this->dir = $dir;
  44. }
  45.  
  46. public function getCount(){
  47.  
  48. if ( is_array($this->dirArray) ){
  49. $this->initialize();
  50. }
  51.  
  52. return $this->count;
  53. }
  54.  
  55. public function getArray(){
  56.  
  57. if ( is_array($this->dirArray) ){
  58. $this->initialize();
  59. }
  60.  
  61. return $this->dirArray;
  62. }
  63.  
  64. protected function initialize(){
  65.  
  66. $this->dirArray = $this->findImages();
  67.  
  68. sort($this->dirArray);
  69. $this->count = count($dirArray);
  70.  
  71. }
  72.  
  73. protected function findImages( $extensions = array('jpg') ){
  74.  
  75. $files = array();
  76. $dirCon = opendir($this->dir);
  77.  
  78. while ($name = readdir($dirCon)){
  79. $current = strtolower(substr($foo, strrpos($foo, '.')+1));
  80. if ( in_array( $current, $extensions ) ){
  81. $files[] = $name;
  82. }
  83. }
  84.  
  85. closedir($dirCon);
  86. return $files
  87. }
  88.  
  89. }
Add Comment
Please, Sign In to add comment