PHP suggestion/rant

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Peec
Forum Commoner
Posts: 33
Joined: Fri Feb 22, 2008 3:58 am

PHP suggestion/rant

Post by Peec »

What do you think?

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Get rid of the $ operator, it's there for nothing. Just indication of a variable, i think the human beings are smart enough to know that it's a variable and not a string.

Type definition

Code: Select all

class Foo{
    public  String barFoo(String test, Array test, Int number){
       String var = 'Test';
       return var;
    }
}
I would LOVE to see type definition of variables in PHP 6/7!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP suggestion/rant

Post by Jonah Bron »

And while we're at it, we can just turn the whole thing into Java!

But seriously, why? By the way, there are reasons for the $, one of which is here.

There's no type declaration because PHP is a dynamically typed language. There's no need. You can do type-hinting for method arguments though.

Both of those changes would also completely break older code.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP suggestion/rant

Post by Benjamin »

Uhh yeah no thanks.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP suggestion/rant

Post by josh »

return return;

enough said. Also then you'd have collisions between constants & variables. FOO does not necessarily equal $FOO
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP suggestion/rant

Post by John Cartwright »

$ is not an operator :D

//What everyone else said
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP suggestion/rant

Post by josh »

Another thing is code completion, it makes it easier to write IDEs presumably, or to parse PHP code. I can type $ and get a popup hint, without necessarily knowing the first letter of the variable. I can use find & replace to replace '$foo' without replacing foo()
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP suggestion/rant

Post by Benjamin »

Not to mention a complete rewrite of all existing PHP code, including the frameworks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP suggestion/rant

Post by John Cartwright »

Screw PHP. Maybe we should just all start using Java. 8)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP suggestion/rant

Post by josh »

Benjamin wrote:Not to mention a complete rewrite of all existing PHP code, including the frameworks.
not necessarily. If they wanted they could make the '$' optional, and keep old code running but also support a new way of doing it. Nothing is impossible. Basically the '$' token removed, but '$' added as an allowed character in variable names. I think they should keep it though, at least for code completion. I can tell the computer that its a variable for purposes of code completion, with one key. I think that's neat. It also makes find & replace easier.
Peec
Forum Commoner
Posts: 33
Joined: Fri Feb 22, 2008 3:58 am

Re: PHP suggestion/rant

Post by Peec »

I support making PeecFW more like Java, but keep the simplicity of installing it. Also make PHP go away from dynamically typed language.

It will be far better web applications around the globe, no software that has buggy features.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP suggestion/rant

Post by Jonah Bron »

Peec wrote:I support making PeecFW more like Java, but keep the simplicity of installing it. Also make PHP go away from dynamically typed language.
Sorry, you are bound to the parameters set by the language you use. If you want to use Java, all you've got to do is add the jar to your class path.
Peec wrote:It will be far better web applications around the globe, no software that has buggy features.
The two items you mentioned will not make people's software less buggy. All programming languages are equally easy to write buggy code in as far as I'm concerned. Humans are the weak link.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP suggestion/rant

Post by josh »

Peec wrote:It will be far better web applications around the globe, no software that has buggy features.
Usually my bugs are logic errors. I can't remember the last time I actually had a bug because I thought something was the wrong data type? lol.. You can pass the wrong data type in either type of language and your program can crash.
Peec
Forum Commoner
Posts: 33
Joined: Fri Feb 22, 2008 3:58 am

Re: PHP suggestion/rant

Post by Peec »

josh wrote:
Peec wrote:It will be far better web applications around the globe, no software that has buggy features.
Usually my bugs are logic errors. I can't remember the last time I actually had a bug because I thought something was the wrong data type? lol.. You can pass the wrong data type in either type of language and your program can crash.
That's because PHP doesn't require you to declare a variable to a datatype (only type-hinting in methods and functions).
This makes us PHP writers write ugly code, using $var as string object. PHP is just too dynamic and messy.
When you declare the datatype you will decrease the chance of getting "logic errors" and "bugs" because you will suddenly know what a variable is, it's purpose and how the variable can be used.
Also writing IDE's for PHP will be even better, and errors will be discovered earlier.

Oh another thing:
get rid off all _ in functions and put it all in objects and classes.

Code: Select all

$str = file_get_contents();
VS

Code: Select all

String str = new \php\File('file.txt')->getContent();
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: PHP suggestion/rant

Post by Jenk »

So remind us, why is it you are using PHP?

As for writing meaningless variables, that happens in every language and type-stricting does nothing to help this, in fact makes it worse because people like yourself falsesly believe it helps and get lazy when naming variables and parameters.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP suggestion/rant

Post by josh »

Peec wrote:When you declare the datatype you will decrease the chance of getting "logic errors" and "bugs" because you will suddenly know what a variable is, it's purpose and how the variable can be used.
You'll know it's type (string vs integer, vs float). In fact it causes more opportunity for bugs (oops thought it was signed, but took the unsigned value, program crashed - this can't happen in PHP). Either way a logic error is something like sending the wrong value (yet correct type), so how exactly does strict typing help prevent logic errors?? By logic error I mean the programmer made a mistake, such as reversing a conditional accidentally during an attempted refactoring. The only thing I know of that helps reduce those kinds of errors are unit tests, which coincidentally are easier & faster to write in a dynamically typed language (no need to add new signatures).

And as for "its ugly" that doesn't even make sense, having less actual code on the screen is "ugly"? You think the more verbose way of writing the same thing is "prettier"?

Your second suggestion of wrapping primitives in objects contradicts your first suggestion of having strict type checking. If I just want an array of strings why should I have the overhead of having the objects? PHP allows you to create your own wrapper object, and just provides the primitive. Its a language, not a framework... Languages that use objects instead of primitives are at a disadvantage (presumably) as far as system resources go. Also if a language enforced usage of some particular class for all primitives, you'd have problem extending it since most languages are single inheritance. This can be seen in Java for example, where stubbing out these classes for unit testing is impossible, and it creates extra boiler plate "factory" code if you intend to extend the built in class later on down the road. (this is true of any class provided as part of the language)

For example consider something like 5.add(10) == 15, no "new" operator, they're not objects, just primitives with an object oriented like operator. Being able to mixin methods from the global namespace or something. What happens then when two libraries need to define a method called .foo() ? Does the latter defined method replace the one that was defined first? It's much easier for the two libraries to use their own class so there is no collision. This way if you want an object oriented interface, you can have one and there is no base class forced on you, and if you don't want object oriented interfaces on your primitives, you don't need one.

And in your post you say "string object", this confuses me
Post Reply