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
Code CritiqueHaving 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.
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];
}
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.
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.
"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
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
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.
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
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.