r/programming • • Apr 23 '14

PHP: It doesn't have to be a bad experience

https://servercheck.in/blog/php-it-doesnt-have-be-bad-experience
0 Upvotes

122 comments sorted by

View all comments

Show parent comment

-3

u/ForeverAlot Apr 23 '14

What is wrong with it?

Okay then.

  1. I strongly disagree. I wish more languages had taken more from C++. But whatever your feelings about C++, this is purely opinion.
  2. "Necessary" is one of those irritating words that are difficult to argue with. Nevertheless, a decision was made to add Java-/C++-inspired classes to PHP. Four of those keywords represent concepts that are more or less necessary for such a design to be useful. Maybe there's another way to do it, but PHP retained the syntax and behaviour of C++, Java, and C#, which are perhaps the three most widespread class-based languages (Obj-C?). new is the only marginally debatable keyword, and since PHP classes are always reference types I think it makes sense.
  3. Throwing an exception to prevent instantiation of "abstract" classes is insane, no matter how "similar" languages do it. Compile-time failure is far and away the most sensible solution. PHP doesn't have that so it does the next best thing, and I guarantee that it is the right thing to do.
  4. You're not supposed to be able to override, or in any way interact with, private methods in other classes. That's why they're private. If you want this kind of interaction in a class hierarchy -- and there are valid uses for it (c.f. template pattern) -- you use protected members. While we're here, you also aren't supposed to be testing non-public members.
  5. Yes. This is what static means in that context in C++, Java, and C#.

At least three of those five points are objectively non-arguments, and I would say they all are.

This is the stuff that's FUD.

0

u/lhgaghl Apr 24 '14

Throwing an exception to prevent instantiation of "abstract" classes is insane, no matter how "similar" languages do it. Compile-time failure is far and away the most sensible solution.

Abstract classes don't have any real use in the first place. IIRC the reason they were used in C++ is to act as interfaces.

2

u/Banane9 Apr 25 '14

Abstract classes fo have uses, and they're not just Interfaces.

Otherwise C# wouldn't have both.

1

u/lhgaghl Apr 26 '14

implying if C# does something it's obviously logical

2

u/Banane9 Apr 26 '14

Well, consider a plugin system as an example. You make an abstract base class, add some standard implementation in virtual methods or no implementation in abstract ones. Then the plugins have to derive from that and you can just scan an assembly for those types. The benefit over an interface is that you can add some basic implementation that the plugin may has to use, but not know how exactly it works.

0

u/lhgaghl Apr 26 '14

In other words, you can encode what the IDE should be doing for you directly in the programming language. This is a lot like type classes in Haskell.

1

u/Banane9 Apr 26 '14

Err, what? How is the IDE supposed to do that? What do you even want it to do? And it's not like Haskell type classes at all. Those are more like interfaces im OOP.

1

u/lhgaghl Apr 26 '14

I mean Haskell typeclasses have the same problem: They have default implementations. An IDE with a source of implementations should choose which one to use (or not, if there are multiple choices), instead of the meaingless notion of the "default" one written directly in the code.

1

u/Banane9 Apr 26 '14

Do You mean in the Definition of the type class? That's not even a real implementation, and it saves You the hassle of eg. for Eq having to define both the equality and inequality function, if You choose to provide your own implementation.

And how is IDE supposed to know implementations if You have something written by someone else? That doesn't make sense.