PHP Syntax - how would you change it?

Beginning coders have trouble with the concept of datatypes. Expert programmers prefer to use them and avoid loose typing when feasible. There’s no clean way around that fact. Hence offer both approaches as I outlined. No one is going to hold a gun to your head and demand you use tolerant or strict vars - and the language would continue working just fine without their use. When you are ready to deal with strict data casting the feature would be there waiting for you. That’s hardly in the way and hardly an increase in complexity.

You don’t program Javascript very often do you? It isn’t mandatory to declare a variable before using it in javascript. Advised, but hardly mandatory.

The danger of not declaring the variable is largely that of scope. When a variable is referenced the interpreter checks each scope up the chain for the object until it finds the variable or hits the window object. If it hasn’t found it when it hits the window object it creates the variable in the local scope and proceeds.

  1. Remove extract(), compact(), register_globals, the global keyword (maybe keep $_GLOBALS).

No. Hell no. Register globals is gone in PHP 6 and that is a good thing - along with it say buh bye to safe mode and magic quotes.

Compact is incredibly useful in debugging - so no, removing that is a very stupid suggestion.

Extract is more convenience than necessary, but helps keep code readable. I use it to very powerful effect in my template engine.

The global keyword is necessary as part of function scoping. Yes, it can be misused to create hidden dependencies in a library or framework, and yes it is bad practice to have a function import a var into scope with global and then change it’s value rather than return it, but it has it’s place in the language.

Just like eval() does for better or worse.

And it’s $GLOBALS, not $_GLOBALS.

Here’s what I’d change in PHP if could:

  1. Make it mandatory to declare a variable before initialising it kinda like Javascript:
var $newvar = 1;

This will prevent a lot of mistakes that could be hard to debug or lead to security holes.

  1. Remove extract(), compact(), register_globals, the global keyword (maybe keep $_GLOBALS).

  2. Think about object literals. Ideally they would create new objects of class stdClass or something.

$myobj = object(x => 4, y => 2);

The actual syntax I used is debatable; to emulate Javascript you could use {x:4, y:2} but the above seemed more consistent with other parts of PHP such as array().

PS. I don’t like many of the previous suggestions. If there’s already a way to do it in PHP then why not leave it as is, for example $this->something is perfectly fine and it’s not as if #something saves any actual lines of code (in this instance, I’d say it makes it less intuitive as well).

What would be the benefit though Tom, just syntactic sugar?

I can certainly see your logic there, but such a change would do more harm than good. The harm is increased complexity. I’m not sure about you, but learning is not fun. If I have to get my hands dirty with a new language, API or environment, it has to be as simple as possible, so I can quickly jump in and get to work. The best languages, frameworks and API’s are based on the less is more approach.

I’ve been having to get my hands dirty with SharePoint recently at work, and my god, I now hate Microsoft has a little more than I already did. It’s the most unnecessarily complex kludge of technologies, which doesn’t seem to utilise any of my 4+ years of web development experience. If it were simple, then the story would be completely different.

I’m with you to some extent. While you should have to open or close PHP code blocks in a file with an extension of PHP, I’d still like a way to hop out of PHP, in case I want to insert a block of text such as HTML without worrying about escaping or anything. That’s all.

There’s a reason that people do this, primarily to enforce separation of concerns while developing. Notably, this allows for someone like a web designer to work on the design of a page while a web developer works on the business logic, and through this design decision they can tie their work together. There are ways to mitigate this sort of vulnerability without figuratively disposing of the baby with the bathwater. Notably, limiting permissions of your web server and never executing user supplied files.

I disagree with this because it would add a non-trivial level of overhead for incredibly minimal gain and would likely be incredibly under-utilized. I’m especially not a fan of anything which forces naming schemes on downstream users.

There has been discussion on a similar topic (parameter hinting, casting or both) for years. Currently, there is one implementation* of scalar type hinting on the development branch of PHP and lots of recent discussion** amongst the core devs about if/how this style of type hinting should/could work. I don’t believe any firm conclusions were reached.

[size=1]

That’s one thing I’d love to see in PHP.

PHP’s library functions can be typed - actually because they’re written in C for the most part - so it’d make sense to allow typed parameters in general PHP. It seems… inconsistent without it.

No.

Whatever value is assigned on the function name is the default if the value in question isn’t present. If it is present whatever value is there will be ignored (including the casted result of it).

That would be awesome! I like it especially because it seems realistic like they might actually do that someday.

I don’t like having to type -> much either because i sometimes misplace - with = (on my keyboards). I do think -> is very easy to read though and looks pretty nice.

I would like to be able to cast inside a function name like this:
function (int $value, int $something) {}

Rather than (which i don’t do by the way, but for example)
function ($value = (int) $value, $something = (int) $something)

Actually is that possible? already?

I don’t like that goto thing in PHP, It feels like Actionscript (kinda, lol), but it seems like a lot of problems could happen breaking the flow :\ I guess since it has to be in the same file thats kinda good.

Oh, that reminds me:

Fix namespaces to be backwards compatible. The design decision taken to make you access the inbuilt classes using \ (e.g. \stdClass ) was stupid. There should be some basic name resolution. E.g. “new SomeClass” will look back through the namespace tree until it finds something with that name, ending, obviously with the global namespace.

As it stands I can’t move a single class to a namespace without breaking it. sigh

If I wasn’t worried about backward compat –

  1. Remove # from being a comment operator. PHP doesn’t need 2 ways to comment code – instead…
  2. Make # the string connotation operator instead of .
  3. Then make . the scope resolution operator as it is in javascript instead of having three scope resolution operators (::, -> and \ )

That would be nice. Impractical, but nice. Now things that can be done that I want.

  1. Placement of all existing functions into a legacy namespace. Choose at ini time whether or not to include the legacy namespace.
  2. Rewrite the function library consistently outside the legacy namespace.
  3. Introduce tolerant and strict variables to use alongside the existing scalar variables. Both of these would have an assigned type. Tolerant variables silently convert data put into them to the proper type, Strict variables emit a E_USER_WARNING if given the wrong type before converting as the tolerants do.

goto has been introduced supposedly so you can jump out of a nested loop when you don’t know how deep it is so can’t use break. Or something.

Sounds like a load of rubbish to me :stuck_out_tongue:

My two big ones have been covered: I’d like to replace -> with the . operator, and I’d like to see (and this is the really big one) Strings become a native object. If you’ve never worked in a language where Strings are an object, then you really don’t know what you’re missing.

You know, I was mulling it over and I have a few changes I’d like to see… Kind of a syntax change, but mostly it’s security stuff.

Remove <?php, <? and ?> from the specification to get rid of the illegible train wrecks of code people build constantly opening and closing them – forcing all .php files to execute as code. (getting rid of readfile in the process). It’s called echo, use it.

Include a interpreter level check to functions to say “this library can only be called from a function named _____ in a parent php file named _____”

Deployment checksums and datestamp checks - so if a file is modified without rebuilding the checksums and timestamps PHP will refuse to execute the file. Hell, just timestamp checking would do.

require/include blacklists and whitelists with wildcard support. That way you can say “never include a file with the extension .jpg” or “only include files with the .php extension” – hell, just the last one.

Actually yeah, make it so that require, require_once, include and include_once can only read in and execute files with the .php extension… and make readfile (if you keep it) unable to ever output the contents of a file with the .php extension. To me that’s something that should have been in there since day one.

In other words, plug the damned security holes that plague damned near every PHP script the moment user uploads are involved.

Though admittedly, at least I’m able to disable readfile from the php.ini

example:
    programming_hell();
/*
 *
 *
 */
goto example;

Phew! I was worried you might have been serious. I haven’t used goto since I programmed a ski game on my Vic-20. That must have been some 27 years ago (eek!).

I really do not understand why they implemented goto in PHP.
Does anybody have any decent, real-life examples of a situation in which goto might be handy?

That’s all I have to say :lol:

Off Topic:

[indent]Quote deathshadow60
Interesting, no?[/indent]

Yes. Could you forward me the details of the windows system so I can try to reproduce it? I just quickly installed XAMPP (1.7.3) on Windows (Vista SP2) running PHP 5.3.1 and cannot reproduce the zero.

I was being sarcastic, I didn’t realize that there was a goto.
I’ve wished for one when I realized I’d made an awful mistake in my structure and logic, but I agree, it’s kind of useless and discourages the discipline that serves you well with the crazy-ish syntax.

Wow, I always regarded the lack of goto as a blessing in disguise to me. I cut my teeth on Apple BASIC and the GOTO was one of the most used commands; that in retrospect seeded very deeply what turned out to be extremely poor and unnecessary coding habits.

That wouldn’t just deliver the visual you suggest; it would be able to parse/execute faster since it would allow the use of three separate reference tables making the lookup faster. Symbol prefixes for names means a faster parser. I’m pretty sure that’s part of why PHP put the $ in front of variables in the first place!

Though I’m not convinced properties need that – the variable namespace prefix of $this-> likely serves the same purpose.