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 14: Line 14:
 
   }
 
   }
 
}
 
}
 +
 
$p = new Person("andrew",99,1.8);
 
$p = new Person("andrew",99,1.8);
 
Echo ($p->bmi());
 
Echo ($p->bmi());
 
Echo ($p->n);
 
Echo ($p->n);

Revision as of 15:01, 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);