To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-29-2004, 05:59 AM   #1
felipe_lopes
Member
 
felipe_lopes's Avatar
 
Join Date: Jan 2001
Location: Brazil
Posts: 84
critique my class plz!

Hi there!

I'd like to get from you guys some guidance on my code. I'm kind of new to OOP on php then I don't feel like I'm programming 100% OOP...

PHP Code:
<?php

class random_phrase {

    var
$file;

    
//
    
function get_num_lines() {

        
$lines = file($this->file);
        
$number = count($lines);

        return
$number;

    }

    
//
    
function random_num() {

        
$lines_num = $this->get_num_lines();
        
$lines_num--;
        
$random_line = rand(0, $lines_num);

        return
$random_line;

    }

    
//
    
function show_phrase() {

        
$line = $this->random_num();
        
$lines = file($this->file);

        print
$lines[$line];

    }

}

?>
I don't like using variables inside a class without the "$this->"...Can I use the $this-> on variables even if the variable is not declared on properties?

Critique my code, plz!
__________________
Felipe Lopes

eForum - no db! - http://felipelopes.dotgeek.org
felipe_lopes is offline   Reply With Quote
Old 01-29-2004, 06:20 AM   #2
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,128
You can, but then the variables become properties of the object. There's no harm just leaving them as variables.

Or you could shorten your code, eg:
PHP Code:
function get_num_lines() {
       return
count(file($this->file));
    }
Too many short and simple lines starts to interfere with readability - a bit like "See Spot Run. See PEBoD kill." - because it looks like there's more going on than there actually is and someone reading the code might worry about not seeing the "rest of what it's doing".

On the other hand - from the perspective of using this class in the wild, I'd consider reading the file itself into the object when it's created - rather than reading and rereading the entire file every time you want to know something like how many lines it has.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 01-29-2004, 06:22 AM   #3
planetsim
code | beer > sleep
 
Join Date: Sep 2002
Location: aus
Posts: 4,826
PHP Code:
<?php
class random_phrase {

    var
$file;
    var
$lines=0;
    var
$totalLines=0;
    
//
    
function get_num_lines() {

        
$this->lines = file($this->file);
        
$this->totalLines = count($this->lines);
        return
$this->totalLines;
    }

    
//
    
function random_num() {
        
srand(microtime() * 1000000000000);
        
$lines_num = $this->get_num_lines();
        
$lines_num--;
        
$random_line = rand(0, $lines_num);

        return
$random_line;

    }

    
//
    
function show_phrase() {

        
$line = $this->random_num();

        print
$this->lines[$line];

    }

}
?>
Your code didnt randomize the file so here is a better version of it. Its not great i probably shouldnt be returning the result in get_num_lines().

Other than that good first attempt at OOP. Maybe we make a database abstraction layer class as your next go at it.
__________________
Dont be lazy Search
And use the Manual
Webmobo - Open Source News Script | Portfolio / Blog | Against TCPA
planetsim is offline   Reply With Quote
Old 01-29-2004, 07:24 AM   #4
felipe_lopes
Member
 
felipe_lopes's Avatar
 
Join Date: Jan 2001
Location: Brazil
Posts: 84
Didn't think about doing a constructor for reading the file...That's a good idea Weedpacket!

Thanks for your help planetsim! The code was working fine though...It is a class to show random phrases written on a txt file.
__________________
Felipe Lopes

eForum - no db! - http://felipelopes.dotgeek.org
felipe_lopes is offline   Reply With Quote
Old 01-29-2004, 07:39 AM   #5
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
Re: critique my class plz!

I did a little work to your class. but I didn't test my changes so there are probably bugs

Change Log:
1) Added initialization code that allows the user to speficy the file in the initialization and checks to make sure the requested file exists.

2) added lines and lines_calced variables so that you don't have to do the costly recalculation of the file size all the time.

3) changed print phrase to get phrase because good oop coding dictates that you should never print from within a class.

Other changes that should be made:
Each function should use the initialized variable to make sure the class was properly initialized before executing.
PHP Code:
<?php

class random_phrase {

    var
$file,$lines,$lines_calced,$initialized,$error;

    
//let's add some code to initialize this class
    //sample call $my_phrase = new random_phrase(file_name.txt);
    
function random_phrase($file) {
        
$this->file = $file;
        if(
file_exists($file)) {
            
$initialized = TRUE;
        } else {
            
$this->initialized = FALSE;
            
$this->error = "Specified file does not exist";
        }
        
$this->lines_calced = FALSE;
    }
//end initialization code

    
function get_num_lines() {
        
$this->lines = count(file($this->file));
        
$this->lines_calced = TRUE;
        return
$this->lines;
    }

    function
random_num() {
         if(!
$this->lines_calced) {
            
$this->get_num_lines();
         }

    
$random_line = rand(0, $this->lines);

    return
$random_line;
    }

    function
get_phrase() {

        
$line = $this->random_num();
        
$lines = file($this->file);

        return
$lines[$line];

    }

}

?>
drawmack is offline   Reply With Quote
Old 01-29-2004, 08:48 AM   #6
felipe_lopes
Member
 
felipe_lopes's Avatar
 
Join Date: Jan 2001
Location: Brazil
Posts: 84
Thanks for your contribution drawmack!

You made my class easier to understand for those who read the code I think....

I'll try to avoid printing from inside a class now on!
__________________
Felipe Lopes

eForum - no db! - http://felipelopes.dotgeek.org
felipe_lopes is offline   Reply With Quote
Old 01-29-2004, 09:41 AM   #7
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,128
Re: Re: critique my class plz!

Quote:
Originally posted by drawmack
3) changed print phrase to get phrase because good oop coding dictates that you should never print from within a class.
Unless of course it's an output handler class
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 01-29-2004, 01:49 PM   #8
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
Hi, my name is weedpacket and I'm an overstater of the obvious.


HI WEEDPACKET

sorry, couldn't resist - yes every rule has it's exception.
drawmack is offline   Reply With Quote
Old 01-29-2004, 10:42 PM   #9
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,128
Quote:
Originally posted by drawmack
yes every rule has it's exception.
Including this one

"Good evening. Your name is Weedpacket and your specialist subject tonight is 'The Bleeding Obvious'."
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 09:13 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.