Click to See Complete Forum and Search --> : My first read OOP project....


Moonglobe
09-30-2003, 12:02 AM
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
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.

laserlight
09-30-2003, 07:03 AM
I find your excessive use of the ternary operator disturbing :p

BuzzLY
09-30-2003, 09:50 AM
Not to mention, it's stretching out the page (I thought only newbies did such a thing ;))

Moonglobe
09-30-2003, 10:46 AM
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?

laserlight
09-30-2003, 11:53 AM
Yeah, the ?: syntax is sometimes called the ternary operator.

Moonglobe
09-30-2003, 12:05 PM
o well, i use it a lot, i find it quite useful and it looks ok too..........

BuzzLY
09-30-2003, 05:09 PM
"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 :)

drawmack
09-30-2003, 08:19 PM
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.

Moonglobe
09-30-2003, 09:44 PM
LOL drawmack........ it WAS properly indented but i cut it down so it wouldn't stretch the screen....c.f BuzzLY's post.

Jeb.
10-01-2003, 06:12 AM
Apart from the ternary operator use (damn, man :D), 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.

Moonglobe
10-01-2003, 10:46 AM
Originally posted by Jeb.
Apart from the ternary operator use (damn, man :D), 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! :)

figment
10-30-2003, 11:58 AM
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

Moonglobe
10-30-2003, 12:24 PM
PHP5 has almost total backwards compatibility, as long as your code doesn't have any of the words listed here (http://www.php.net/zend-engine-2.php) used for functions etc then you should be okay :)

hth
moon