Learning PHP and MySQL

Example 05-16 Accessing the $age variable using this-->

<?php
class  Cat  {
  
//  How  old  the  cat  is var  $age;

  //  Constructor
  
function  Cat($new_age)
  
{
    
//  Set  the  age  of  this  cat  to  the  new  age  
    
$this->age  =  $new_age;
  
}
  
//The  birthday  method  increments  the  age  variable 
  
function  Birthday()
  
{
    $this
->age++;
  
}
}
//  Create  a  new  instance  of  the  cat  object  that’s  one  year  old
$fluffy=new  Cat(1);
echo  
"Age  is  $fluffy->age  <br/>";
echo  
"Birthday<br/>";
//  Increase  fluffy’s  age
$fluffy->Birthday();
echo  
"Age  is  $fluffy->age  <br/>";
?>

Posted by on 08/03 at 08:24 AM

Next entry: Example 05-17 Using the extends keyword to define a subclass

Previous entry: Example 05-15 Adding the $age variable to Cat

<< Back to main