oregon Posted December 9, 2008 Share Posted December 9, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/136164-solved-oop-test-not-working-what-am-i-doing-wrong/ Share on other sites More sharing options...
gevans Posted December 9, 2008 Share Posted December 9, 2008 <?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); ?> Link to comment https://forums.phpfreaks.com/topic/136164-solved-oop-test-not-working-what-am-i-doing-wrong/#findComment-710205 Share on other sites More sharing options...
oregon Posted December 9, 2008 Author Share Posted December 9, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/136164-solved-oop-test-not-working-what-am-i-doing-wrong/#findComment-710215 Share on other sites More sharing options...
gevans Posted December 9, 2008 Share Posted December 9, 2008 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. Link to comment https://forums.phpfreaks.com/topic/136164-solved-oop-test-not-working-what-am-i-doing-wrong/#findComment-710222 Share on other sites More sharing options...
oregon Posted December 9, 2008 Author Share Posted December 9, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/136164-solved-oop-test-not-working-what-am-i-doing-wrong/#findComment-710224 Share on other sites More sharing options...
gevans Posted December 9, 2008 Share Posted December 9, 2008 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); ?> Link to comment https://forums.phpfreaks.com/topic/136164-solved-oop-test-not-working-what-am-i-doing-wrong/#findComment-710225 Share on other sites More sharing options...
oregon Posted December 9, 2008 Author Share Posted December 9, 2008 why would i need to add the "public"? i think it is by default public if not declared. is there any special reason you added that? Maybe i am just confused! Link to comment https://forums.phpfreaks.com/topic/136164-solved-oop-test-not-working-what-am-i-doing-wrong/#findComment-710227 Share on other sites More sharing options...
gevans Posted December 9, 2008 Share Posted December 9, 2008 No, you don't need to have it there, it's just for convenience (I like having it there ) Link to comment https://forums.phpfreaks.com/topic/136164-solved-oop-test-not-working-what-am-i-doing-wrong/#findComment-710232 Share on other sites More sharing options...
oregon Posted December 9, 2008 Author Share Posted December 9, 2008 I can see the benefit of marking it. Just so the code is more clear I would assume. Thanks. No, you don't need to have it there, it's just for convenience (I like having it there ) Link to comment https://forums.phpfreaks.com/topic/136164-solved-oop-test-not-working-what-am-i-doing-wrong/#findComment-710234 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.