jd2007 Posted September 2, 2007 Share Posted September 2, 2007 <?php class Arrays { private $arr=array(); private $mystr; function produceStr(array $arr) { $this->arr=$arr; $this->mystr=implode("and", $this->arr); echo $this->mystr; } } ?> when i instantiate an object, i do this: <?php $a=new Arrays(); $b=array("a", "b", "c"); $a->produceStr($b); ?> this doesn't work...why ? Quote Link to comment https://forums.phpfreaks.com/topic/67656-how-do-i-pass-array-as-an-argument-for-a-function/ Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 There is no object of type array in php so I don't believe you can type hint using it. eg; This... function produceStr(array $arr) should simply be... function produceStr($arr) Other than that, I don't really see any problem. Quote Link to comment https://forums.phpfreaks.com/topic/67656-how-do-i-pass-array-as-an-argument-for-a-function/#findComment-339844 Share on other sites More sharing options...
jd2007 Posted September 2, 2007 Author Share Posted September 2, 2007 but i want to be array $arr but php treats it like string Quote Link to comment https://forums.phpfreaks.com/topic/67656-how-do-i-pass-array-as-an-argument-for-a-function/#findComment-339848 Share on other sites More sharing options...
wildteen88 Posted September 2, 2007 Share Posted September 2, 2007 Code works fine for me, using PHP5.2.x here. I noticed you are using the private keyword here for initiating variables. Make sure you are not using PHP4. PHP4 does not support the visibility keywords (private, protected and public), use var in replacement: var $arr=array(); var $mystr; What results do you get? EDIT: Didn't see the replies above but i want to be array $arr but php treats it like string Even if the you pass the produceStr function an array for the $arr argument $arr will still be an array, without 'array' before $arr. Quote Link to comment https://forums.phpfreaks.com/topic/67656-how-do-i-pass-array-as-an-argument-for-a-function/#findComment-339849 Share on other sites More sharing options...
Daniel0 Posted September 2, 2007 Share Posted September 2, 2007 wildteen: You can use array for type hinting as well. Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported. Quote Link to comment https://forums.phpfreaks.com/topic/67656-how-do-i-pass-array-as-an-argument-for-a-function/#findComment-339876 Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 wildteen: You can use array for type hinting as well. Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported. That was me. And thanks, something I was not aware of. Quote Link to comment https://forums.phpfreaks.com/topic/67656-how-do-i-pass-array-as-an-argument-for-a-function/#findComment-339878 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.