News for hackers. Stuff that matters.
Re:PHP solves problems. Oh, and you can program with it too by Joeri on Friday June 29, 2012 @05:49PM (#4179284)
I think PHP's problems are actually quite solvable without core language changes. The OO model is actually pretty good, there's even reflection functionality that's sufficient to do stuff like auto-generating docs and self-validating DTO objects. The issue are the "legacy" api's, and the primitive types. What's missing are object api's wrapping the primitive and legacy functionalities. Those could be built quite easily though.

You could have something like jQuery's $ function, but for PHP variables, that gives you an OO api on top of that value (especially now that you can use the -> operator on return values of functions without intermediate assignment). I wonder why nobody's built that yet. I suppose because those in the PHP community are all busy programming instead of meta-programming.

Re:PHP solves problems. Oh, and you can program with it too by whichdan on Friday June 29, 2012 @06:09PM (#4179375)
Do you mean something like this?

  function s($string) {
    return new String($string);
  }

  class String
  {
    public $string;

    public function __construct($string) {
      $this->string = $string;
    }

    public function __toString() {
      return $this->string;
    }

    // several helper methods like the following:

    public function toLower() {
      $this->string = strtolower($this->string);
      return $this;
    }
    
    // this would create lots of extra objects,
    // but leave the original unchanged
    public function alternateToLower() {
      return new String(strtolower($this->string));
    }
  }

  $username = s('ExampleUser');
  echo $username->toLower();
My concern would be the memory used to create several dozen extra objects. If this is indeed what you're talking about, is there any strong benefit besides convenience?
Re:PHP solves problems. Oh, and you can program with it too by Joeri on Saturday June 30, 2012 @02:18AM (#4180897)
Yes, lots of memory, much worse performance, but it doesn't really matter. If you care that much about those, you're not using any interpreted language.

Besides, most PHP code is linear without much looping other than iterating database records. The overhead of such an approach wouldn't matter that much, and when it does you can always use the old way of doing things.

But you wouldn't need to create an object per call, you could reuse a singleton that gets initialized by a wrapper function to work on your current variable (using references). Fix($str)->replace("foo", "bar") is a syntax that doesn't require object instantiation or string copying. And the singleton could use the mb functions internally to be multibyte aware.

Re:PHP solves problems. Oh, and you can program with it too by zeroonetwothree on Friday June 29, 2012 @07:07PM (#4179618)
One reason to abstract away strings is that you can gain security. For example, your template might only accept an HTMLString, which can only be created from a String by escaping. Your database library might only accept a QueryEscapedString, and so on.

Of course all of this is possible without creating a wrapper for vanilla strings.

Re:PHP solves problems. Oh, and you can program with it too by Ralith on Friday June 29, 2012 @06:31PM (#4179476)
> PHP's problems are actually quite solvable without core language changes

How do you fix weak typing?

Re:PHP solves problems. Oh, and you can program with it too by Joeri on Saturday June 30, 2012 @02:12AM (#4180881)
Javascript has weaker typing than PHP (in php you can have typed parameters for object parameters), but javascript is the new hotness and php gets no love. Typing is not the issue that makes php so ugly.

Also, i write php all day and i almost never run into bugs due to typing. You adopt some habits like always casting before use, and use an ide that warns you when you're doing stuff like using uninitialized variables (phpstorm is awesome), and the whole issue goes away for 95%. I'm not saying it doesn't cause bugs, just that i've found it to not be that big of a deal.

Re:PHP solves problems. Oh, and you can program with it too by 10098 on Friday June 29, 2012 @06:50PM (#4179542)
Ostensibly, by writing lots and lots and lots of unit tests. In practice, however, no one has time to write or update unit tests. Which is bad, but understandable, because circumstances can be pressing. In that case, the only fix for weak typing is praying that nothing blows up :-)
Re:PHP solves problems. Oh, and you can program with it too by Ralith on Friday June 29, 2012 @06:55PM (#4179570)
While I appreciate that your post is mostly jest, it's worth noting that a "fix" that places an immense amount of additional load on the programmer is not worthy of the title (in part due to the consequences you describe). And even then, even the best unit tests rarely cover every case.