Jump to content

types in function declarations


shiftJIS

Recommended Posts

So, here's a watered down snippet of what I've been working on:

class SOMETHING{  public $TEXT;  public function some_method(string $var){	$this->TEXT = $var;  }  public function __construct(){	$this->some_method("default");  }}

This has an error "Fatal error: Argument 1 passed to SOMETHING::some_method() must be an object of class string, called in.."So, the thing is, if I changed that function some_method to some_method($var), it'd work as intended. But the results aren't what I expected them to be. Doesn't calling the function with ("default") pass a string?Any ideas what's up?

Link to comment
Share on other sites

I was wondering that as well. I think the key point in the error is that it says an "object of class string", rather then a variable of type string. I don't think there is a built-in string class, and so it is looking for a custom class and not finding one. You can probably do the same thing by explicity casting it as a string, maybe like this:public function some_method((string) $var){But, it looks like it is getting into a subtlety of the language that I haven't seen, but I'm assuming it is looking for a class called string.

Link to comment
Share on other sites

I think you must have the string declaration in parenthesis. If you are transferring from Java to PHP then that might be the mistake. You dont have to declare types of variables. There is a function called by doing is_string("string") that will return true or false if it is a string or not. You could you that?

Link to comment
Share on other sites

Haha, I guess everybody is as confused as me.Actually, the habit is coming from C, where you have to type all of your variables and functions. I'm actually not all that specific about typing in php, but my current project will be offered to the public eye, so I've been trying to take all steps into making it clean and legible to others.I suppose my confusion starts at the php docs:

addslashes(PHP 3, PHP 4, PHP 5)addslashes -- Quote string with slashesDescriptionstring addslashes ( string str )
As you notice, they describe all the functions and tell you 1) the type the function returns, and 2) the types it takes as arguments.I do know that we don't have to declare types of variables, but I wanted to - I suppose what I wanted was for the functions I declared to throw an error if it was sent the wrong type (that is, without having to rely on the error catching code).Also, in the IDE that I use, functions you declare get added to the autocomplete list. My neurotic side wanted the little description to include those types.Anyhow, I suppose I'll leave it be for now and just stick with what works. Thanks for everybody's input!--UpdateGrr, If I only knew what to look for in the docs. Seems the answer was right there:http://us3.php.net/manual/en/language.oop5.typehinting.phpQuote from the little text at the bottom:
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.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...