Hey all,

I've been working on my first OPP driven PHP code and wanted to see what
everyone thought and any ideas anybody can add to the code that I'm working
on.  I've had to make some concessions due to the limitations of PHP's OOP
model and the fact that I'm using PHP3 which doesn't functions like
get_class or get_parent so I'll have to explicity add those myself.  In my
example here, I've provided a Base Class which everything inherits from.  It
provides Methods for Getting and Setting variables, which checking to making
sure those variables exist in the object declaration before setting them.
Please let me know you think.

Thanks
Joel

-------------------------------------------------------------------
/*  BaseClass.inc */
<?php

class BaseClass {
 var $_Class;

 function BaseClass() {
  $this->Set("_Class", "BaseClass");
 }

    function Get($VarName) {
  return $this->$VarName;
 }

 function AddVar($VarName, $Value = "") {
  $this->$VarName = "";
 }

 function Set($VarName, $Value) {
  if (!isset($this->$VarName)) {
   new Error("?", "?", "Invalid Variable Name at method call " .
$this->Get("_Class") . "->Set($VarName, $Value).");
  } else {
   $this->$VarName = $Value;
  }
 }
}

?>

/* Math.inc */
<?php

class _Math extends BaseClass {

 var $_Seed;

 /* Math (private) */
 /*   A series of math related functions needed for the performance of other
objects */
 /* Parameters: */
 /*   None */

 function _Math() {
  $this->Set("_Class", "Math");
  $this->Set("_Seed", (double) microtime() * 1000000);
 }

 /* SeedRandomNumbers (private) */
 /*   Seeds the Random Number Generator */
 /* Parameters: */
 /*   None
 /* Returns: */
 /*   None */
 function _SeedRandomNumbers() {
  mt_srand($this->Get("_Seed"));
 }

 /* RangeOfRandomNumbers (private) */
 /*   Returns a random number between a high and low number inclusive */
 /* Parameters: */
 /*   Low - Lowest possible number of Random Number */
 /*   High - Highest possible number of Random Number */
 /* Returns: */
 /*   (Integer) Random Number between two numbers inclusive */

 function _RangeOfRandomNumbers($Low = 1, $High = 6) {
  return mt_rand($Low, $High);
 }

 function _GCD($m,$n) {
  if ($n==0) {
   return $m;
  } else {
   return ($this->_GCD($n,($m%$n)));
  }
    }

} //End Math

/* Fraction.inc */

<?php

class Fraction extends _Math {

 var $_WholePart;
 var $_Numerator;
 var $_Denominator;

 function Fraction ($Whole, $Numerator=0, $Denominator=0) {
  $this->Set("_Class", "Fraction");
  $this->Set("_WholePart", $Whole);
  $this->Set("_Numerator", $Numerator);
  $this->Set("_Denominator", $Denominator);
 }

 function Add ($AddTo) {
  $Num = ($this->Get("_Numerator") * $AddTo->Get("_Denominator")) +
($AddTo->Get("_Numerator") * $this->Get("_Denominator"));
  $Den = ($this->Get("_Denominator") * $AddTo->Get("_Denominator"));
  if ($Num != 0 && $Den != 0) {
   $GCD = $this->_GCD($Num, $Den);
   $Num = $Num / $GCD;
   $Den = $Den / $GCD;
   $Whole = floor($Num / $Den);
   $Num = $Num % $Den;
  }
  $NewFraction = ($Num != 0) ? new Fraction($this->Get("_WholePart") +
$AddTo->Get("_WholePart") + $Whole, $Num, $Den) : new
Fraction($this->Get("_WholePart") + $AddTo->Get("_WholePart") + $Whole);

  return $NewFraction;
 }

 function Fetch() {
  $frac = "";
  if ($this->Get("_WholePart")) {
   $frac .= $this->Get("_WholePart") . " ";
  }
  if ($this->Get("_Numerator")) {
   $frac .= $this->Get("_Numerator") . "/" . $this->Get("_Denominator");
  }
  $frac = chop($frac);
  if (empty($frac)) {
   $frac = "0";
  }
  return $frac;
 }

 function Show() {
  print $this->Fetch();
 }

}

-------------------------------------------------------------------
There are worlds out there where the sky is burning, the seas
sleep, and the rivers dream; people made of smoke, and cities made of
song.  Somewhere there's danger; somewhere there's injustice, and somewhere
else the tea is getting cold!  Come on Ace, we've got work to do!



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to