The longer I work with PHP, the more I want this in PHP next: Strict

At the time of this writing PHP next would be PHP 5.7. Ultimately this would need to go to the PHP request for contribution. But before wasting time there with something poorly thought out. I’d rather hash things out some.

Strict Variables.
Strict Variables would be a class of PHP variables that will not allow themselves to have their datatype changed once declared. They must also be explicitly declared with the strict keyword. If you try to create them without the keyword then the engine assumes you meant scalar for legacy reasons. The declaration looks like this.

// in procedural code
strict integer $a = 5;

// in a class
class MyClass {
  public strict integer $a = 2

If a strict variable is compared to a scalar with a different datatype using the loose comparison operator, the scalar must recast.

strict integer $a = 5;
$b = '5';

echo $a == $b ? 'true' : 'false'; // will echo 'true'

If two strict variables are compared recasting won’t be allowed, so the loose comparison operator will be forced to behave like a strict comparison operator.

strict integer $a = 5;
strict string $b = '5';

echo $a == $b ? 'true' : 'false'; // will echo 'false'

If a strict variable is assigned to a value of a different type, that value will be recast and a E_USER_WARNING will be raised.

strict integer $a = 5;

$a = '2'; // raises warning.

The warning can be avoided with a new “cast and assign” operator, similar in concept to the existing error suppression operator and using the same @ symbol which is otherwise unused in PHP.

strict integer $a = 5;

$a @= '3';  // cast '3' to the datatype of the strict variable $a before assigning to $a.

Finally, as a shorthand the programmer can assign datatype and value together by using the strict keyword alone.

strict $a = 5;  // $a will be an integer since it's being assigned an integer value.

Strict variables hold their datatype until unset. They would provide a system for experienced programmers to explicitly manage and control datatypes without interfering with the ability of novice programmers to ignore the concept of datatype until they are ready to tackle it.


I think you want to read this:

Instead of wishing that a language had features it (probably) never will, why not just find one that does? There are lots of them to choose from and learning more languages never hurt anyone and there’s no reason to try and shoehorn every solution into PHP. I personally don’t like dynamically typed languages in general, it just feels wrong to me, thankfully there are many that aren’t. Scala is a very nice hybrid approach that handles typing very elegantly.

'cause you have to understand that many people are either more comfortable with PHP or they are stuck with PHP. The former is less of a problem, since good coders can easily get used to new languages they aint exposed to, but the latter is quite different. What if you are a web developer for a company that uses PHP? What if you dont have absolute authority to decide the programming language/platform to use? There are many reasons people stick to PHP, its not practical to just tell everyone just to migrate to a different language.

Not practical to migrate to a different language, but you don’t have to migrate to a new language to study it and learn it. I never said to migrate to it, I just suggested learning something new… at least that’s what I meant anyway. I can see how my wording could be taken differently.

Even in large companies any developer can make a big difference… even if it’s just by starting a small wave in a big pool. You’re not going to make much of a splash with something you’re uncomfortable with.

Yeah I was not talking about learning a new language, but migrating to/adopting a new language in the software you develop. I mean to say that some programmers are stuck with PHP so they have to work with it, but they aint happy since PHP is missing some features they expect to have. Maybe we aint talking about the same thing, I know you make a good point but I was just offering an explanation why some people would wish for a feature in PHP when there are other languages available.

Im not sure why strict would help if adhering to good OOP development principles in coding.
With object properties you can specify a class attribute as read only anyway. (Can’t remember if PHP does this but I know python does.)

I personally like hybrid languages the least. Especially if they’re hybrid over their decision to be staticly or dynamicaly typed. I wish they would pick a side and stick with it.

Sent from my XT316 using Tapatalk 2

If you’re referencing my post, Scala is statically typed but uses type inference.

Maybe we aint talking about the same thing, I know you make a good point but I was just offering an explanation why some people would wish for a feature in PHP when there are other languages available.

Many many companies have spent years creating a ecosystem of PHP software to run their business. Rarely is there value in just up and switching to a new language given the costs associated with it. For newer projects perhaps. However, even then every time new technologies are introduced it makes things more complex from an operations stand point which again costs money.

Having said that I don’t really see the point of strict variables in PHP myself. Every language is going to have it’s ups and downs. If the negative outweighs the positive than find a job using another language or perhaps insist on some level of change based on modern technology.

There is something to be said about moving between different languages but most companies don’t it seems. The cost from an operations stand-point becomes to high. Not to mention if worse comes to worse and developers need to be cut it is much more likely that a small team can support a consistent ecosystem of projects sharing the same technology than those that are in all different types of languages. I guess it all depends on what the service model and purpose of the company is though.

Which brings me to why I believe extending the language is a good option when it comes to some things.