Difference between revisions of "PHP:Define a Class"

From Progzoo
Jump to: navigation, search
Line 1: Line 1:
class Person {
+
class Person {
 
   public $name;
 
   public $name;
 
   private $weight, $height;
 
   private $weight, $height;
Line 13: Line 13:
 
     return $bmi;
 
     return $bmi;
 
   }
 
   }
}
+
}
  
$p = new Person("andrew",99,1.8);<br />
+
$p = new Person("andrew",99,1.8);
Echo ($p->bmi());
+
echo ($p->bmi());
Echo ($p->n);
+
echo ($p->n);

Revision as of 15:08, 12 February 2009

class Person {
 public $name;
 private $weight, $height;
 public function __construct($name, $weight, $height)
 {
   this->name = $name;
   this->weight = $weight;
   this->height = $height;
 }
 public function bmi()
 {
   $bmi = $this->weight / $this->height / $this->height;
   return $bmi;
 }
}
$p = new Person("andrew",99,1.8);
echo ($p->bmi());
echo ($p->n);