Difference between revisions of "PHP:Define a Class"

From Progzoo
Jump to: navigation, search
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{topTenTips}}
 +
<question className='Hello' lang='php'>
 +
The class Person has:
 +
*a constructor: <code>__construct</code>
 +
*attributes <code>name, weight</code> and <code>height</code>
 +
*a method: <code>bmi</code>
 +
<prog><![CDATA[
 +
<?
 
class Person {
 
class Person {
 
   public $name;
 
   public $name;
Line 4: Line 12:
 
   public function __construct($name, $weight, $height)
 
   public function __construct($name, $weight, $height)
 
   {
 
   {
     this->name = $name;
+
     $this->name = $name;
     this->weight = $weight;
+
     $this->weight = $weight;
     this->height = $height;
+
     $this->height = $height;
 
   }
 
   }
 
   public function bmi()
 
   public function bmi()
Line 15: Line 23:
 
}
 
}
  
$p = new Person("andrew",99,1.8);
+
  $p = new Person("andrew",99,1.8);
Echo ($p->bmi());
+
  print ($p->bmi()."\n");
Echo ($p->n);
+
  print ($p->name);
 +
?>
 +
]]></prog>
 +
</question>

Latest revision as of 14:16, 19 February 2009

The class Person has:

  • a constructor: __construct
  • attributes name, weight and height
  • a method: bmi


[Font] [Default] [Show] [Resize] [History] [Profile]