Difference between revisions of "PHP:Define a Class"

From Progzoo
Jump to: navigation, search
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{topTenTips}}
 
{{topTenTips}}
 
<question className='Hello' lang='php'>
 
<question className='Hello' lang='php'>
The class Person has  
+
The class Person has:
*a constructor: <code>Person</code>
+
*a constructor: <code>__construct</code>
*attributes <code>n, w</code> and <code>h</code>  
+
*attributes <code>name, weight</code> and <code>height</code>  
 
*a method: <code>bmi</code>
 
*a method: <code>bmi</code>
<prog> class Person {
+
<prog><![CDATA[
 +
<?
 +
class Person {
 
   public $name;
 
   public $name;
 
   private $weight, $height;
 
   private $weight, $height;
 
   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 19: Line 21:
 
     return $bmi;
 
     return $bmi;
 
   }
 
   }
}
+
}
  
$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>
+
?>
 +
]]></prog>
 
</question>
 
</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]