Dzherzinsky Posted September 1, 2011 Author Share Posted September 1, 2011 Hello I just copied and pasted what you wrote, here is a full copy of paste of the files in question. I agree with you that your code made perfect sense, so I dont know where I am screwing up <?php class Motorbike extends Vehicle { protected $bikes_speed = array(); /*public function speed ($d1, $d2, $d3) { // the 'this' keyword denotes that the variable $bikes_speed belongs to the current object $this->bikes_speed[] = $d1/10; $this->bikes_speed[] = $d2/10; $this->bikes_speed[] = $d3/10; return $this->bikes_speed; }*/ public function speed($speeds) { foreach($speeds as $speed) { $this->bikes_speed[] = $speed/10; } return $this->bikes_speed; } } ?> and this is the other file <?php require_once 'vehicle.php'; require_once 'Motorbike.php'; $v1 = 50; $v2 = 20; $v3 = 600; $suzuki = new Motorbike(); $speeds = $suzuki->speed($v1, $v2, $v3); echo "Speeds are: "; foreach($speeds as $speed) { echo $speed . "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246058-function-that-returns-several-values-how-to-oop-call-it/page/2/#findComment-1264471 Share on other sites More sharing options...
KevinM1 Posted September 1, 2011 Share Posted September 1, 2011 The problem is that you're not passing an array into your object. In your script, you need: $v = array(50, 20, 600); $suzuki = new Motorbike(); $speeds = $suzuki->speed($v); echo "Speeds are: "; foreach($speeds as $speed) { echo $speed . "<br />"; } Remember: a class is merely a blueprint, much like a function definition. It doesn't become real until you instantiate it (create an object of your class' type) by calling "new classname();" Just because you define a method (class function) to take an array as a parameter, that doesn't mean it magically happens. You still need to construct the array and pass it into the method in the script yourself. Quote Link to comment https://forums.phpfreaks.com/topic/246058-function-that-returns-several-values-how-to-oop-call-it/page/2/#findComment-1264481 Share on other sites More sharing options...
Dzherzinsky Posted September 1, 2011 Author Share Posted September 1, 2011 alright, so having the 3 arguments and sending them to an array does not work like when you assign the items to an array like $my_array = array(1, 2, 3); I thought that it would have been the same, gosh, sorry for so much inconvenience. I promise to watch more the forum and ask less. I have learned a lot from you best regards Quote Link to comment https://forums.phpfreaks.com/topic/246058-function-that-returns-several-values-how-to-oop-call-it/page/2/#findComment-1264488 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.