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 09-30-2003, 12:02 AM   #1
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
My first real OOP project....

well, as most of those reading this forum will know, i recently created the PEBoD classes using php5.......and i was wondering if you guys could find anything wrong in the way i've coded things........ and suggestions for additions are welcome too .
PHP Code:
<?php
class PEBoD
{
    
public $cage;
    
public $hunger;
    
public $anger;
    
protected $hunger_array;
    
protected $time_to_eat;
    
protected $feedback;
    
protected $e_code;
    
protected $free;    
    
protected $anger_array;
    
    
public function __construct()
    {           
        
$this->cage         =& new PEBoD_Cage(&$this);
        
$this->hunger_array = range(0,200,2);
        
$this->hunger       = $this->hunger_array[0];
        
$this->feedback     = "";
        
$this->anger_array  = range(1,100);
        
$this->anger        = $this->anger_array[0];
    }
   
    
private function contemplate_life($length=10800)
    {
        
$now = time();
        if (
$length < $now)
        {
            
$then = time()+$length;
        }
        else
        {
            
$then = $length;
        }
        while (
$now < $then)
        {
            
sleep(1);
            
$now++;
        }
    }
    
    
private function sf($str="")
    {
        
$this->feedback  = $str;
        
$this->e_code[1] = $str===""?false:true;
        return
true;
    }
    
    
public function feedback()
    {
        return
$this->e_code[1]?$this->feedback:"No Feedback";
    }
    
    
public function put_in_cage()
    {
        if (
$this->free)
        {
            if (
$this->cage->is_open())
            {
                
$this->free = false;
                
$this->cage->fill();
                return
true;
            }
            else
            {
                
$this->sf("Cannot put beaver in closed cage");
                return
false;
            }
        }
        else
        {
            
$this->sf("Beaver already in cage!");
            return
false;
        }
    }
    
    
public function is_hungry()
    {
        if (
$this->hunger > 100) return true;
        return
false;
    }
    
public function feed(&$food)
    {
        
srand(make_seed());
        
$ranking = is_object($food)?
                                    
21
                                    
:is_resource($food)?
                                                        
18
                                                        
:is_array($food)?
15:is_string($food)?12:is_float($food)?9:is_int($food)?6:is_bool($food)?
3:is_null($food)?0:0;
        
$hunger_index = ($this->hunger -($ranking + rand((0 - 3),3)))
        
$this->hunger = $this->hunger_array[$hunger_index];
        unset(
$GLOBALS[array_search($food,$GLOBALS)]);
        unset(
$food);
    }
    
    
public function is_angry()
    {
        
srand(make_seed());
        if ((
$this->anger + rand((0 - 3),3)) < 51) return true;
        return
false;
    }
}   

################################################################################

class Cage
{
    
public $filled;
    
protected $feedback;
    
protected $e_code;
    
protected $open;    
    
    
public function __construct()
    {
        
$this->open     = false;
        
$this->feedback = "";
        
$this->e_code   = array(0,0);        
    }
    
    
public function feedback()
    {
        return
$this->e_code[1]?$this->feedback:"No Feedback";
    }
    
    
public function open()
    {
        if (
$this->open)
        {
            
$this->sf("Cage already open!");
            
$this->e_code[0] = 1;
            return
false;
        }
        else
        {
            
$this->open = true;
            
$this->sf("Cage opened successfully!");
            
$this->e_code[0] = 0;
            return
true;
        }
    }
    
    
public function close()
    {
        if (!
$this->open)
        {
            
$this->sf("Cage already closed!");
            
$this->e_code[0] = 2;
            return
false;
        }
        else
        {
            
$this->open = false;
            
$this->sf("Cage close successfully!");
            
$this->e_code[0] = 0;
            return
true;
        }
    }
    
    
public function is_open()
    {
        return
$this->open;
    }
    
    
public function is_closed()
    {
        return !
$this->open;
    }
    
    
public function is_full()
    {
        return
$this->full;
    }
    
    
public function is_empty()
    {
        return !
$this->full;
    }
    
    
private function sf($str="")
    {
        
$this->feedback = $str;
        
$this->e_code[1] = $str===""?false:true;
        return
true;
    }
    
    
public function fill()
    {
        
$this->full = true;
        return
true;
    }
}
################################################################################
class PEBoD_Cage extends Cage
{
    
private $parent;
    
private $crack;
    
public function __construct(&$input)
    {
        if (!
is_object($input) && get_class($input) !== "pebod")
        {
            
$this->sf("Input not a proper object!");
            return
false;
        }
        if (!
$input->cage->is_parented()) $this->parent =& $input;
        
parent::__construct();
        
$this->crack = 10.25;
    }
    
    
public function is_parented()
    {
        return isset(
$this->parent);
    }
    
    
public function shake()
    {
        
srand(make_seed);
        
$is_free = rand(1,100)>=$this->parent->anger?false:true;
        if (
$is_free)
        {
            
$this->full = false;
            
$this->parent->free = false;
            
$this->parent->sf("Beaver has been released!");
            
$this->sf("Beaver has been released!");
            return
"HAHA!";
        }
        else
        {
            
$this->parent->anger += 10;
            
$this->sf("Hey! You were lucky this time!");
            return
true;
        }            
    }
    
    
public function pull_in(&$food,$size='')
    {
        if (!
$size && $size !== 0) $size = (strlen($food)+5)/10;
        if (
$size > $this->crack) return false;
        
$this->parent->feed($food);
    }
}
?>
btw these are Parse Error-Free in PHP5b2-dev.
__________________
there's no place i can be, since i found serenity.

Last edited by Moonglobe; 09-30-2003 at 10:54 AM.
Moonglobe is offline   Reply With Quote
Old 09-30-2003, 07:03 AM   #2
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,396
I find your excessive use of the ternary operator disturbing
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 09-30-2003, 09:50 AM   #3
BuzzLY
2($infinity) && $beyond
 
BuzzLY's Avatar
 
Join Date: Nov 2002
Location: Star Command
Posts: 2,535
Not to mention, it's stretching out the page (I thought only newbies did such a thing )
__________________
New to the board? Check out the guidelines
| Color Picker | Blogification |
¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
With all its sham, drudgery, and broken dreams, it's still a beautiful world.
BuzzLY is offline   Reply With Quote
Old 09-30-2003, 10:46 AM   #4
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
heh, sorry it was late........... and my gawd i was tired after cleaning the house for several hours and i didnt think about it..........changing..........
and BTW hate to sound stupid but what's the ternary operator? do you mean the ?: syntax?
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 09-30-2003, 11:53 AM   #5
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,396
Yeah, the ?: syntax is sometimes called the ternary operator.
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 09-30-2003, 12:05 PM   #6
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
o well, i use it a lot, i find it quite useful and it looks ok too..........
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 09-30-2003, 05:09 PM   #7
BuzzLY
2($infinity) && $beyond
 
BuzzLY's Avatar
 
Join Date: Nov 2002
Location: Star Command
Posts: 2,535
"Ternary" means having three parts. It is related to the words "unary" (one part), and "binary" (two parts). So, because there are three parts to the operation:

CONDITION ? TRUE : FALSE

That is why it's called a "ternary operation." Technically, any if statement can be a ternary operation:

if CONDITION {
TRUE
} else {
FALSE
}

The else, if not included, is implied. It's as if you said "else do nothing."

However, because the short version requires both the TRUE and FALSE parts of the operation, it got the name "ternary operation."

Leave it to a geek to give a fancy name to such an operation -- "3-part operation." I guess someone just wanted to sound smart, and the name stuck
__________________
New to the board? Check out the guidelines
| Color Picker | Blogification |
¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
With all its sham, drudgery, and broken dreams, it's still a beautiful world.
BuzzLY is offline   Reply With Quote
Old 09-30-2003, 08:19 PM   #8
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
PHP Code:
    public function feed(&$food)
    {
        
srand(make_seed());
        
$ranking = is_object($food)?
                                    
21
                                    
:is_resource($food)?
                                                        
18
                                                        
:is_array($food)?
15:is_string($food)?12:is_float($food)?9:is_int($food)?6:is_bool($food)?
3:is_null($food)?0:0;
        
$hunger_index = ($this->hunger -($ranking + rand((0 - 3),3)))
        
$this->hunger = $this->hunger_array[$hunger_index];
        unset(
$GLOBALS[array_search($food,$GLOBALS)]);
        unset(
$food);
    }
like indenting much?

If you use spaces for indenting it will stop things like this from happening.
drawmack is offline   Reply With Quote
Old 09-30-2003, 09:44 PM   #9
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
LOL drawmack........ it WAS properly indented but i cut it down so it wouldn't stretch the screen....c.f BuzzLY's post.
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-01-2003, 06:12 AM   #10
Jeb.
Titles are overrated.
 
Jeb.'s Avatar
 
Join Date: Jul 2003
Posts: 150
Apart from the ternary operator use (damn, man ), you haven't initialised all of your class variables in the Cage constructor, and the same with PEBoD constructor.

I suggest turning that ternary block into a switch statement. Use get_type(), and check the result.
__________________
Once you eliminate the impossible, whatever remains, however improbable, must be the truth. - Sir Arthur Conan Doyle
Jeb. is offline   Reply With Quote
Old 10-01-2003, 10:46 AM   #11
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Quote:
Originally posted by Jeb.
Apart from the ternary operator use (damn, man ), you haven't initialised all of your class variables in the Cage constructor, and the same with PEBoD constructor.

I suggest turning that ternary block into a switch statement. Use get_type(), and check the result.
thanks, ill check for that.... and ya, i guess that would be easier on the eyes. thx!
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-30-2003, 11:58 AM   #12
figment
Enygma: Rubiks Cuban
 
Join Date: Nov 2001
Location: Hampstead, MD
Posts: 77
Question PHP 5

I realize that this is the wrong forum for this post/reply, and I aplologize up front.

But I noticed that you said you used PHP 5, and i am starting the proccess of re-installing/updating my web server at home, and I am wondering how much backwards compatability PHP 5 has. I would like to start using it, but I am worried that in doing so, I will end up writing code that I cant putup to my webhost untill they upgrade.

Cany anyone provide some insight into this? thanks

:Figment
__________________
:Figment
<figmentenygma.com>
figment is offline   Reply With Quote
Old 10-30-2003, 12:24 PM   #13
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
PHP5 has almost total backwards compatibility, as long as your code doesn't have any of the words listed here used for functions etc then you should be okay

hth
moon
__________________
there's no place i can be, since i found serenity.
Moonglobe 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 03:46 PM.






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.