TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   How good is my coide (http://www.talkphp.com/advanced-php-programming/3793-how-good-my-coide.html)

geo353 12-21-2008 08:59 PM

How good is my code
 
Hello,

Ive been programming in php for a few years now but never really took any notice of how practical or versatile my code is. Im currently working on building myself a portfolio site (something ive been meaning to for a long time). Please take a look at a couple of snippets of my code and give me some feedback.

PHP Code:

<?php

/*********************************************************************************
 *       Filename: portfolioManager.lib.php
 *       Version 1.0
 *       Copyright 2007 - 2009 (c) George Palmer
 *       Last modified: 20 Dec 2008
 *********************************************************************************/

require_once('../inc/lib/dbManager.lib.php');
/*********************************************************************************/
    
class article implements IteratorAggregate
    
{
        protected 
$article;
        
        public function 
__construct($article NULL)
        {
                
$this->article $article;
        }
        
        public function 
__get($property)
        {
            if(isset(
$this->article[$property]))
            {
                return 
$this->article[$property];
            } else {
                return 
false;
            }
        }
        
        public function 
__set($property$value)
        {    
                
$this->article[$property] = $value;
        }
        
        public function 
getIterator()
        {
            return new 
ArrayObject($this->article);

        }
    }
/*********************************************************************************/
    
class portfolio implements IteratorAggregate
    
{
        protected 
$articles;
        
        public function 
__construct($articles NULL)
        {
            if(
$articles != NULL)
            {
                
array_push($this->articles$articles);
            }
        }    
        
        public function 
displayALL($template)
        {
            foreach (
$this->getIterator() as $article)
            {
                
$articleT = new templater($template);
                
$articleT->build($article->getIterator());
                
$articleT->publish();
            }
        }
        
        public function 
displayRand($template)
        {
            
$key array_rand($this->articles);
            
$article $this->articles[$key];
            
$articleT = new templater($template);
            
$articleT->build($article->getIterator());
            
$articleT->publish();
        }
        
        public function 
__set($articles NULLarticle $article)
        {
            
$this->articles[] = $article;
        }
        
        public function 
getIterator()
        {
            return new 
ArrayObject($this->articles);
        }
    }
/*********************************************************************************/

?>

The article class is takes a list of properties and their values from the database row. This object is then stored in the portfolio object for each row.

Depending on the page and result set the articles are displayed like so.

PHP Code:

/*********************************************************************************
 *       Filename: home.inc.php
 *       Version 1.0
 *       Copyright 2007 - 2009 (c) George Palmer
 *       Last modified: 21 Dec 2008
 *********************************************************************************/
    //header template
        
$headerT = new templater('templates/header.tem.php');
            
$headerV = array('TITLE' => page::getPageName(false),
                             
'HEAD'     => page::getPageName(true));
            
$headerV = new ArrayObject($headerV);
        
$headerT->build($headerV);
    
//end header template
    
    //footer template
        
$footerT = new templater('templates/footer.tem.php');
            
$footerV = array('FOOT' => 'Your viewing the '.page::getPageName(true).' page on '.date("D jS, g:i a").'.');
            
$footerV = new ArrayObject($footerV);
        
$footerT->build($footerV);
    
//end footer template
/*********************************************************************************/
    //libarys required
        
require_once('../inc/lib/portfolioManager.lib.php');
    
//end libarys
/*********************************************************************************/
    //start page
        
$dbPortfolio = new portfolioConnect//connect to the portfolio database
        
$articles $dbPortfolio->fetchAll();//fetch all articles
        
        
$headerT->publish();//publish header
        
        
$articles->displayRand('templates/articleSingle.tem.php');//display all articles using template
        
        
$articles->displayAll('templates/articleAll.tem.php');//display all articles using template
        
        
$footerT->publish();//publish footer
    //end page
/*********************************************************************************/ 

or

PHP Code:

/*********************************************************************************
 *       Filename: home.inc.php
 *       Version 1.0
 *       Copyright 2007 - 2009 (c) George Palmer
 *       Last modified: 21 Dec 2008
 *********************************************************************************/
    //libarys required
        
require_once('../inc/lib/portfolioManager.lib.php');
    
//end libarys
/*********************************************************************************/
    //start page before header template in order to fetch title
        
$articleID $_GET['a']; //unclean!!!!!!!!!!!
        
        
$dbPortfolio = new portfolioConnect//connect to the portfolio database
        
$article $dbPortfolio->fetchSingle($articleID);//fetch single article
    //part end page
/*********************************************************************************/
    //header template
        
$headerT = new templater('templates/header.tem.php');
            
$headerV = array('TITLE' => 'George Palmer &raquo; Articles &raquo; '.$article->title,
                             
'HEAD'     => $article->title);
            
$headerV = new ArrayObject($headerV);
        
$headerT->build($headerV);
    
//end header template
    
    //footer template
        
$footerT = new templater('templates/footer.tem.php');
            
$footerV = array('FOOT' => 'Your viewing the "'.$article->title.'" article on '.date("D jS, g:i a").'.');
            
$footerV = new ArrayObject($footerV);
        
$footerT->build($footerV);
    
//end footer template
/*********************************************************************************/
    //start page
        
        
$headerT->publish();//publish header
        
        
$articleT = new templater('templates/articleDetailed.tem.php');
        
$articleT->build($article->getIterator());
        
$articleT->publish();
        
        
$footerT->publish();//publish footer
    //end page
/*********************************************************************************/
?> 

Im also looking for a way to implement paging into this, if anyone has any suggestions.

Many Thanks
George Palmer

Tanax 12-21-2008 09:27 PM

For one thing, I like that it's clean :-)
Don't have time right now to comment the actual code.
But here's a pagination class that I wrote if you're interested!

http://www.talkphp.com/script-giveaw...ion-class.html

You'll probably find a really easy way to integrate this!
Keep on coding!

Crazymik3 12-22-2008 05:00 PM

You're code is nice, put it seems very thick.

Maybe space it out a little more, as it looks really congested right now with so many comments.

geo353 12-22-2008 07:39 PM

cheers tanax i'll have a look at that. crazymik3 iknow its really thick its just how i like to work, means i can fit more on my screen. as for the comments, good point i dont really need most of them, if any.

cheers


All times are GMT. The time now is 05:52 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0