PHP's dynamic typing, should it stay or should it go?

With the coming of PHP7, we now have the possibility to be strict with variable types. This basically changes how PHP does its type juggling, which, IMHO, is just a source for confusion and even worse, weird bugs.

Or is it?

Vote and also give your 2 cents. :smile:

  • Dynamic typing should stay
  • I am on the fence
  • Stricter typing is better for the language overall

0 voters


Well the issue with PHP is not dynamic typing, its weak typing. Note dynamic typing doesnt necessarily lead to weak typing, its possible to have a combination of dynamic and strong typing, such as Python and Ruby. PHP’s nature as a dynamically typed language is why I like it over static typed languages such as Java, it should stay the way it is.

How do you use the dynamic typing to your or the client coder’s benefit?


I would prefer strict typing, but have never in my limited experience had a situation where something had to be flexible in terms of its type, so to me it isn’t that much of an issue. That’s the reason I am on the fence. The only problem I see is the possible confusion that a shift like that could result in as people are once again in the position of having to change their thinking and way of working with the language.

I understand what you mean, but I would disagree. Type juggling itself is a clear source for confusion. I still don’t mess with type coercion and haven’t ever needed it. Maybe I am missing out on something, but I have yet to find someone who can explain any kind of real usefulness, which I might win through flipping types or allowing “not quite correct” types to enter the scene in the middle of a running program.

To me (and I might be wrong on this, as I don’t even want to pretend I am an expert at all) dynamic typing in PHP is simply a way to get a page out of the server in some fashion, despite a programmer plucking together some not so kosher code. PHP was meant to be “forgiving”, sort of like how browsers don’t balk completely, when the HTML isn’t totally correct. They still deliver a page. This “forgiveness” was great many years ago. One might even say it helped PHP grow in popularity, because even novice programmers could put together something that just worked.

Now, PHP needs to “grow up” and I think a lot that has been done in PHP7 is heading in that direction, which is a very good thing IMHO. The strict typing mode is one of them.


Okay, so I’m on the fence, but I truly believe PHP will head the direction of Dynamic and Static types eventually. Will it be soon? I doubt it, but within a decade, probably.

I personally would use PHP a lot more, if it were dynamic and statically typed. Why? Because I don’t have to deal with a bunch of edge cases. What if someone passed in X and I wanted Y, will PHP handle it, or do I have to do a bunch of extra logic to figure it out?

With dynamic and static typed variables, I can be assured what I am receiving and how I can utilize it. I don’t have to worry about those edge cases of someone doing something stupid, as it will throw an exception saying it was the wrong argument type.

Maybe I prefer this because this is what I use every day. So I could be biased, but I just don’t bother to do a lot of PHP work because prior to PHP 7/5, it was hard to do these checks and feel comfortable about it. Once hinting, return types, and what not came about, it helped, as I could enforce rules on my code and if you passed the wrong type, well you be damned, not me. But that is exactly why I think PHP will eventually head the direction of dynamic/static typed variables. They’ve already made approaches to help devs to stop worrying about what idiotic thing someone may pass into your code (with hinting), and what you might get returned.

Just my thoughts.

Hm, isn’t Dynamic an opposition to Static? If so then how PHP could head in the direction of dynamic and static types at the same time if those are two opposing directions? These terms are a bit confusing…

As to the preferred direction for PHP I tend towards leaving the type system as it is while improving checks like the new hints and return types. Both type systems have their pros and cons and I don’t think PHP needs to switch direction completely. My experience with static languages is limited to Java - and on the one hand it’s very nice to have strict types everywhere because the IDE can detect so many errors on the fly while typing but on the other hand sometimes it takes lots of coding to achieve the same result compared to PHP.

However, I’m all for restricting the current type juggling behaviour so as to get rid of implicit type conversions that don’t make much sense. For example, it’s good that I can concatenate strings with integers and floats, or supply a numeric string where an integer or float is required - those are common sense conversions that save a lot of unnecessary typing. However, I don’t think it makes any sense to convert an array to string, concatenate a string with a boolean or array, treat non-numeric strings as 0 in arithmetic or treat strings like ‘12X’ as a valid number - such automatic conversions should throw an exception.

Maybe my terminology is wrong, but this is how I see dynamic versus static typed (both are used this way in C# at least).

var credentials = new Credentials(); //dynamic
Credentials credentials = new Credentials(); // static

Dynamic can be inferred by what is assigned, the assignee can even be a variable and its type will be inferred. Static, specifies the type right up front.


Yeah, I messed up that terminology, but dynamic does exist in C# too

And more info on how var works

Dynamic typing is, you start off with…



$foo = "0"; 
$foo += 2;   
$foo = $foo + 1.3; 
$foo = 5 + "10 Little Piggies"; 

and end up with the integer 15. :stuck_out_tongue_winking_eye:


Oh, so in PHP it isn’t even falling in line with what others are doing… I saw that (your example) as more so weak typing.


Either way, I see PHP heading down the same path C# is at, within the next decade.

Actually, I see no problem with this coming to PHP in a future release. We already have hints for IDE:

/* @var $credentials Credentials */

that are purely informational - it would be much nicer to replace them with real type hints that enforce variable types. But I think they would need to be optional in PHP.

They are optional at the moment. For example:

function test(boolean $param) {}

You can optionally leave out boolean from that function declaration and then $param can be any type

Strict typing is also optional. You must include declare(strict_types=1); as the first line of the file to use it.

I can’t see either being anything but optional as they are currently implemented for quite some time. There is too much older code that would need rewriting were they to be made mandatory now.

Misunderstanding - I was talking about this kind of type hinting:

Credentials credentials = new Credentials();

that is not available in PHP now.

Looks like we are at about 50/50 on this. Interesting.


Yes it is - you just need three lines of code to do it instead of one.

function testCredentials(Credentials credentials) {}
credentials = new Credentials();

But that doesn’t do the same thing persay, that is hinting with a function, which definitely does exist. What I was referring to was hinting on variables (or rather, making a variable expect a specific type).

Credentials credentials = new Credentials();

Credentials credentials = ThirdParty::CredentialRepo::GetCredentials();

$thirdPartyCred = new ThirdParty::Credentials();
Credentials credentials = $thirdPartyCred->GetCredentials();

With your sample code you’ve just proven that it is not available in PHP :smiley: You can emulate any kind of language feature with elaborate code but this does not mean it is part of the language.

In what way doesn’t it do the same thing? If you assign the correct variable type in both cases the code will accept it and if you assign an incorrect variable type then the code will fail in both cases. The only difference is that you currently need to use a function call to test if the variable was assigned the correct type and crash in that call rather than crashing on the assignment itself.

But I also showed that it is trivially easy to implement and so people who want it don’t have to wait for it to be implemented into PHP itself in order to be able to implement it themselves.

It would not have been anywhere near as easy to implement in older versions of PHP.

But that is the difference. You shouldn’t have to call a function to do that. That does nothing but add to execution time.

No, that isn’t trivial, as you’d have to do it with each type you need to do it for. That’s a lot of exceess code and calls that are best not to occur and to just wait till it exists as an actual feature in PHP.