Jump to content

Consolidating redundant code...


cgm225

Recommended Posts

I have two classes I implement three different times, in the same way, with only slight variation...  I want to know, would you (and, if so, how) consolidate this code somehow (in a function or another class?)?

 


        /* Instantiates new Photos class, then gathers all entries from the
         * database, places that data into an array, and then passes the object
         * to the registry
         */ 
        $videos = new Videos($this->mysqli, $this->albumDir);
        $videos->findVideos(); 
        $this->registry->set('videosObject', $videos);
        
        /* Instantiates new Pagination class, passes all the required data for
         * generating pagination, and then passes the object to the registry
         */
        $pagination = new Pagination('videos');
        $pagination->setTotalItems($videos->getDataCount());
        $pagination->setDefaultDisplayNumber($this->registry->get('videosDefaultDisplayNumber'));
        $pagination->setCurrentPageNumber($this->page);
        $this->registry->set('paginationObject', $pagination);

...SNIP... THEN ELSEWHERE...

        $albums = new Albums($this->mysqli);
        $albums->findAlbums();
        $this->registry->set('albumsObject', $albums);

        $pagination = new Pagination('albums');
        $pagination->setTotalItems($albums->getDataCount());
        $pagination->setDefaultDisplayNumber($this->registry->get('albumsDefaultDisplayNumber'));
        $pagination->setCurrentPageNumber($this->page);
        $this->registry->set('paginationObject', $pagination);

...SNIP... AND ELSEWHERE STILL...

        $photos = new Photos($this->mysqli, $this->albumDir);
        $photos->findPhotos(); 
        $this->registry->set('photosObject', $photos);

        /* Instantiates new Pagination class, passes all the required data for
         * generating pagination, and then passes the object to the registry
         */ 
        $pagination = new Pagination('photos');
        $pagination->setTotalItems($photos->getDataCount());
        $pagination->setDefaultDisplayNumber($this->registry->get('photosDefaultDisplayNumber'));
        $pagination->setCurrentPageNumber($this->page);
        $this->registry->set('paginationObject', $pagination);

 

 

Link to comment
https://forums.phpfreaks.com/topic/121143-consolidating-redundant-code/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.