tags:

views:

161

answers:

4

For example, is it possible to write code like this:

int $x = 6;
str $y = "hello world";
bool $z = false;
MyObject $foo = new MyObject();

And is it possible to define functions like this:

public int function getBalance()
{
   return 555; //Or any numeric value
}
+1  A: 

No. That syntax will not work.

You could, theoretically, come up with a system of objects that enforced their own sort of strict typing, but it wouldn't perform and ...why would you want to, anyway?

If you need strict typing, use a strictly typed language.

Frank Farmer
I would love to use a strictly typed language but my clients only want to use php :(
Click Upvote
Thank your clients ... or curse them ... as the case may be.
pavium
If a strictly typed language is really the only right tool for the job, you might consider pushing back. If you hired a carpenter, you wouldn't tell him to use only a screwdriver.
Frank Farmer
Yep, but web development jobs for java appear to be scarce compared with PHP, also the infrastructure for java web apps is a bit more complex/expensive than a LAMP shared hosting..
Click Upvote
+6  A: 

PHP is not strictly typed, so no. That said, it does support limited type hinting on functions - that's as close as it gets.

Alex Barrett
+5  A: 

No. There is support for type hinting since php5 , but "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 is as far as php currently goes, and as far as it should go if you ask me.

code_burgar
A: 

Since the answer is basically "no", an alternative: A PHP "linter", which should catch some of the things a compile-time check would catch in a staticly-typed language like C. Not the same, but should prevent some sillyness

"Is there a static code analyzer [like Lint] for PHP files" lists many of these.

dbr