Jump to content

Data type specified for function argument doesn't work right in CLI mode


Slips

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.