Live data from Hacker News

Everything you need (and don't need) to know about PHP's type system

thephp.website

1–10 of 67 posts

Re: Everything you need (and don't need) to know about PHP's type system

#4
This is quite complete, definitely bookmarking for reference.

I've been knee deep in PHP for the last 6 months after keeping it at arm's length for probably 10 years. It's really quite incredible how much it's improved and I think the type system is the major factor for me. It's not perfect but damn - I can get some stuff done really quick now.

Re: Everything you need (and don't need) to know about PHP's type system

#5
post #4

This is quite complete, definitely bookmarking for reference. I've been knee deep in PHP for the last 6 months after keeping it at arm's length for probably 10 years. It's really quite incredible how much it's improved and I think the type system is the major factor for me. It's not perfect but damn - I can get some stuff done really quick now.

I'll add though; the real benefit only comes with heavy use of objects, like in Yii/Craft. If you've got keyed arrays everywhere it's still a hellhole.

Re: Everything you need (and don't need) to know about PHP's type system

#6
post #5
post #4

This is quite complete, definitely bookmarking for reference. I've been knee deep in PHP for the last 6 months after keeping it at arm's length for probably 10 years. It's really quite incredible how much it's improved and I think the type system is the major factor for me. It's not perfect but damn - I can get some stuff done really quick now.

I'll add though; the real benefit only comes with heavy use of objects, like in Yii/Craft. If you've got keyed arrays everywhere it's still a hellhole.

Arrays can become collections.

Re: Everything you need (and don't need) to know about PHP's type system

#7
While we are on the subject, does anyone know of a way of having required variable declarations in PHP, similar to the way Perl's "use strict" works?

This is one of my most common sources of bugs, and it is frustrating not having this be available, when it makes my life so much easier in Perl.

Lately I've been thinking about doing something crazy like implementing my own variable system...

Re: Everything you need (and don't need) to know about PHP's type system

#8

While we are on the subject, does anyone know of a way of having required variable declarations in PHP, similar to the way Perl's "use strict" works? This is one of my most common sources of bugs, and it is frustrating not having this be available, when it makes my life so much easier in Perl. Lately I've been thinking about doing something crazy like implementing my own variable system...

> Strict typing mode. In PHP the declare(strict_types = 1); directive enables strict mode. In strict mode, only a variable of exact type of the “type declaration” will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a float.

https://www.brainbell.com/php/strict-type.html

Re: Everything you need (and don't need) to know about PHP's type system

#9
Adding a static analyzer (like Psalm [1]) to the mixture makes PHP's types much more powerful. Psalm can use docblock annotations to attach more complex types to functions and variables, including:

* A "list" type representing a non-associative array

* Types representing specific kinds of values, like numeric-string or non-empty-array, as well as exact values as types (like true and false, or string constants).

* Typing on keys and values in arrays, e.g. array for an array with class names as keys and integers as values

* Complex typing on the contents of associative arrays, e.g. array{foo:int, bar:string}

* Templated types on functions, like a function which returns an instance of a class whose name is passed in as a string:

    /**
     * @template T
     * @param T::class $class_name
     * @return T
     */
[1]: https://psalm.dev/

Re: Everything you need (and don't need) to know about PHP's type system

#10
It actually makes me really happy to see more positive comments on a thread about PHP. It is an incredible workhorse, and doesn't get the credit it deserves.

I'm often amused by developers that revile PHP, while going on to use another language, which suffers from a similar set of problems to PHP, oftentimes with poorer performance and more complex toolchains.

PHP deserves a little more love, imo.

Post reply on HN