What does HackerNews think of scalar_objects?

Extension that adds support for method calls on primitive types in PHP

Language: PHP

I kinda hope PHP would adopt java-like types for strings etc, to somewhat clean up the mess of its functions, e.g for strings - instead of str_contains, strtr, substr_compare you can do $myString->contains("test");

https://github.com/nikic/scalar_objects provides it as a plugin, but its not fully fleshed out.

> That would help, I would argue, with killing many of the PHP-specific criticisms

I don't see why appeasing critics should be the goal of PHP.

Removing $ and ; signs doesn't help me at all. I actually like the "$" sign. Maybe little bit less ;, but I don't really notice it.

But I would certainly notice the mess of splitting language syntax into two separate branches.

Breaking changes in language are not fun to address and take away the time from building features.

I would be a lot more happy with generics. Or scalar objects, something like:

$string = "abc";

var_dump($string->length()); // int(3)

var_dump($string->startsWith("a")); // bool(true)

https://github.com/nikic/scalar_objects

That would make PHP standard library a lot easier and fun to use.

It is possible to add that, one of of the core developers has done that as an extension, with one limitation, not possible for a mutable api.

https://github.com/nikic/scalar_objects

However scalar methods also creates a bigger discussion around the existing standard library, should functions be duplicated? Etc.

Better in my opinion, because the standard library is mostly functions in the global namespace is to add pipe operator, it has been voted on but not passed yet due to some limitation.

Pipe operator is more flexible because you can use your own functions when chaining.

And then of course there are libraries that does this, which is similar to how Java does it, compare float vs Float.

A trivial example:

  $len = mb_strlen("wtf");
    # call function mb_strlen

  $len = s("wtf")->len();
    # call function s
      # allocate string object
      # instantiate string object
      # call string ctor
        # set property
    # call method len
        # get property
      # call function mb_strlen
    # destroy object
There's no way to use a syntax like this without either massive overhead, or massive changes in the engine.

There is a somewhat similar experimental project - https://github.com/nikic/scalar_objects - but that's not meant for real world use either.

Personally I think the language, despite its flaws, does its job fine. I don't think it needs turning into Javascript-with-sigils.