jordanwb Posted March 7, 2008 Share Posted March 7, 2008 Error: Catchable fatal error: Argument 1 passed to playlist::add_song() must be an instance of string, string given, called in E:\xampp\htdocs\test\index.php on line 11 and defined in E:\xampp\htdocs\test\playlist.php on line 25 class playlist { private $songs; function playlist ($songs = null) { if ($songs != null) { if (is_array ($songs)) { $this->songs = array (); foreach ($songs as $index => $song_path) { if (is_string ($song_path)) { $this->songs[] = $song_path; } } } } } function add_song (string $path) { } } I'm making it so that you can only pass in a certain type, and you know not pass in garbage. It's seems that my string is not good enough for the parameter. Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/ Share on other sites More sharing options...
Stooney Posted March 8, 2008 Share Posted March 8, 2008 you could try using gettype() to check if its string or strval() to convert to string. Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-486597 Share on other sites More sharing options...
jordanwb Posted March 8, 2008 Author Share Posted March 8, 2008 But I'm passing in a string would I need to do that? Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-486608 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 I didn't even know PHP supported type specific function params.... Then I tested some stuff, and I don't think it does. function test(SomeClass $var) { echo $var->DoSomething(); } class SomeClass { public function DoSomething() { } } $sc = new SomeClass(); test($sc); When ever you "type $param," it interprets it as a class type, not a native type (string, int, so on). Anyway, you'll have to make sure it's a string inside the function. Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-486767 Share on other sites More sharing options...
jordanwb Posted March 8, 2008 Author Share Posted March 8, 2008 Ok Thanks. [Edit] Where'd the "Topic Solved" button go? Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-487010 Share on other sites More sharing options...
Daniel0 Posted March 8, 2008 Share Posted March 8, 2008 The only non-object type you can type hint is an array. Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-487023 Share on other sites More sharing options...
jordanwb Posted March 8, 2008 Author Share Posted March 8, 2008 How would I go about doing that? <? function foo (array $bar){} ?> Like that? Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-487052 Share on other sites More sharing options...
Daniel0 Posted March 8, 2008 Share Posted March 8, 2008 Exactly. Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-487061 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 Edit: Daniel obviously beat me too it, but oh well... posting it anyway ;p Exactly. (I never knew you could hint types at all lol x.x tested this just now) When you put the array there, the input must be an array or it throws a catchable fatal error. I expected if you passed it a single string it would convert it to an array with one element, but no, it doesn't. I guess that's because it figures if you strictly want an array, it should strictly check it, not try to make things an array. Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-487066 Share on other sites More sharing options...
jordanwb Posted March 8, 2008 Author Share Posted March 8, 2008 Thanks. Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-487085 Share on other sites More sharing options...
keeB Posted March 9, 2008 Share Posted March 9, 2008 You can still do it with a string, fyi Pretty sure this works <?php function lol (string $a) { echo $a; } $mystr = (string) "hello, world!"; lol($mystr); ?> Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-487521 Share on other sites More sharing options...
Daniel0 Posted March 9, 2008 Share Posted March 9, 2008 You can still do it with a string, fyi Pretty sure this works <?php function lol (string $a) { echo $a; } $mystr = (string) "hello, world!"; lol($mystr); ?> Did you run the code? Catchable fatal error: Argument 1 passed to lol() must be an instance of string, string given, called in C:\apache2\htdocs\test.php on line 7 and defined in C:\apache2\htdocs\test.php on line 3 You cannot type hint scalar values. Link to comment https://forums.phpfreaks.com/topic/94986-solved-must-be-an-instance-of-string-string-given-wait-what/#findComment-487559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.