Jump to content

[SOLVED] oop test not working - what am i doing wrong?


oregon

Recommended Posts

hello,

i am running through a book and experimenting with php oop, and for some reason I cannot get this class echoing properly. I tried several variations, checked for typos, etc.. But no luck :( ..

 

<?php 

class Car
{ 
public $gas = 0;
function addGas($amount)
{ 
	$this->gas = $this->gas + $amount;
		echo "$amount gallons added to the tank!";
}
}

$mycar = new Car;
$gas_amount = $mycar->gas;

?>

<?php 

class Car
{ 
   public $gas = 0;
   function addGas($amount)
   { 
      $this->gas = $this->gas + $amount;
         echo "$amount gallons added to the tank!";
   }
}

$mycar = new Car;
$gas_amount = $mycar->gas;

?>

 

Your only assigning a variable there, if you want to add some gas do this;

 

<?php 

class Car
{ 
   public $gas = 0;
   function addGas($amount)
   { 
      $this->gas = $this->gas + $amount;
         echo "$amount gallons added to the tank!";
   }
}

$mycar = new Car;
$gas_amount = $mycar->gas;
$mycar->addGas(20);

?>

gevans,

 

that worked perfect. i see what you mean. i think it is also true i can exclude the:

$gas_amount = $mycar->gas;

 

Now I tried to make the var and function private, and I am getting a php error:

Fatal error: Call to private method Car::addGas() from context '' in C:\xampp\htdocs\jasons\php\4.php on line 14

 

<?php 

class Car
{ 
   private $gas = 0;
   private function addGas($amount)
   { 
      $this->gas = $this->gas + $amount;
         echo "$amount gallons added to the tank!";
   }
}

$mycar = new Car;
$mycar->addGas(20);

?>

private functions and variables in classes can only be accessed by the class itself, not outside it. Try changing the function to public, you can leave the variable as private, though you won't be able to access the variable from outside the class.

thanks gevans,

 

i see i can only access the function within the class, but i can use the new function to access the value, and then i can call the value out as i did before like so:

 

<?php 

class Car
{ 
   private $gas = 0;
   private function addGas($amount)
   { 
      $this->gas = $this->gas + $amount;
         echo "$amount gallons added to the tank!";
   }
        function buyGas($amount) // new function
   {
	$this->addGas($amount);
   }
}

$new_car = new Car;
$new_car->buyGas(20);

?>

exactly, though I would add a couple more things;

 

<?php 

class Car
{ 
   private $gas = 0;
   private function addGas($amount)
   { 
      $this->gas = $this->gas + $amount;
         echo "$amount gallons added to the tank!";
   }
   public function buyGas($amount) // i say a couple, just one see 'public' before the function!!
   {
      $this->addGas($amount);
   }
}

$new_car = new Car;
$new_car->buyGas(20);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.