Slips Posted December 11, 2010 Share Posted December 11, 2010 Hello all, I just created this function that I want to use in CLi mode. It works perfectly in regular browser mode, but gives me this error on Cli mode if I do convertToCamelCaps('TEST STRING'); PHP Catchable fatal error: Argument 1 passed to convertToCamelCaps() must be an instance of string, string given in file... if (!function_exists('convertToCamelCaps')) { function convertToCamelCaps(string $string, $initialCaps=FALSE) { if ($words = explode(' ',$string)) { foreach ($words as $index => &$word) { $word = strtolower($word); if($index==0 && $initialCaps===FALSE) { continue; } $word = ucwords($word); } return implode('',$words); } return FALSE; } } If I remove the string datatype requirement in the function before the function argument list, it works fine in CLi mode. Link to comment https://forums.phpfreaks.com/topic/221311-data-type-specified-for-function-argument-doesnt-work-right-in-cli-mode/ Share on other sites More sharing options...
Anti-Moronic Posted December 11, 2010 Share Posted December 11, 2010 The error is saying it is treating $string as an instance of string, as if string was a class. I never really specify data types in php because it is loosely typed - I wouldn't do it in the function parameters. Validate the data type after, within the function. Until support is properly implemented for such things, it is usually a bad idea to try to force a half-assed feature in php. In this instance, data types. Have you tried to encase 'string' in parentheses? function convertToCamelCaps((string) $string, $initialCaps=FALSE) Not sure that will validate the data type, but it will convert the data over to a string. Link to comment https://forums.phpfreaks.com/topic/221311-data-type-specified-for-function-argument-doesnt-work-right-in-cli-mode/#findComment-1145705 Share on other sites More sharing options...
Slips Posted December 11, 2010 Author Share Posted December 11, 2010 I tried (string) before the argument name and I got this error : PHP Parse error: syntax error, unexpected T_STRING_CAST, expecting '&' or T_VARIABLE in ... Had no idea PHP's type specifying was weak. Thought i'd make my code more solid by doing this in all my functions if this one worked. Oh well, guess i'll just do it inside the function. Thanks for the tip. Link to comment https://forums.phpfreaks.com/topic/221311-data-type-specified-for-function-argument-doesnt-work-right-in-cli-mode/#findComment-1145707 Share on other sites More sharing options...
Anti-Moronic Posted December 11, 2010 Share Posted December 11, 2010 I tried (string) before the argument name and I got this error : PHP Parse error: syntax error, unexpected T_STRING_CAST, expecting '&' or T_VARIABLE in ... Had no idea PHP's type specifying was weak. Thought i'd make my code more solid by doing this in all my functions if this one worked. Oh well, guess i'll just do it inside the function. Thanks for the tip. Ahh, sorry. Thought it might work. The general use of (string) or (int) is to convert a data type over to that specified type. It is used like this: $number = (int) 'somestring1'; Which returns 1 So it isn't really for validating types, but instead converting to types. Because PHP is loosely typed you don't have to specify types, they are assumed implicitly. But, you can ensure they are of correct type by using the is_() family of functions. http://php.net/manual/en/function.is-string.php if(!is_string($string)){ // says is not string, so return function or log error. } There may be some type testing classes or libraries out there somewhere. You can see some info on php type hinting here: http://php.net/manual/en/language.oop5.typehinting.php Link to comment https://forums.phpfreaks.com/topic/221311-data-type-specified-for-function-argument-doesnt-work-right-in-cli-mode/#findComment-1145721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.