Itay Grudev
Itay Grudev

Reputation: 7424

PHP '->' into '.'

Is it possible to change the object operator "->" to ".", so accessing class methods would look like that: $user.add() instead of $user->add()?

I guess that this is not a very smart question, but you have to agree, that when you type "->" You have two symbols for one of which you have to press SHIFT which is ways more complicated then ".". And the dot looks prettier.

Upvotes: 0

Views: 170

Answers (6)

Lion
Lion

Reputation: 17879

Basically PHP is open source, so you can get the source code and modify as you wish. But in the reality such a modification is a bad idea because of a couple of reasons:

  • Note that the dot is a php-operator! By default, $foo.bar() will be intepreted as connection between the variable $foo and the return type of the function bar() so it would be necessary to also change the dot-operator to something else, maybe a plus like it is handled in other languages
  • Every other PHP-Developer which may want to check your code or work with you because of projects or some other reason will be confused because of these changes
  • On every PHP-Update you've to apply your changes on the new version and maybe edit your changes because they're not compatible with the new version any more - both will result in extra-work, especially on security-updates this is bad
  • Maybe you'll be work as a PHP-Developer in the future, then you're confused because of working in a company which is using the default syntax

Your primary idea is not bad to replace these two chars by one, but you see that these changes will be result in some other problems, which are not in a relationship to the time you save with this modification. I'm myself a PHP-Developer and I can tell you that using -> instead of a dot will confusing you at the beginning, especially when you're comming from other languages like Java or C# which are using the dot.

But after writing some code you get familiar with these type of syntax and it shouldn't be a problem any more. You can put your forefinger of your right hand on the '-' key, your little finger on the shift key right of the minus and the little finger of your right hand to the '|' key. Then it will not cause you much overhead to type a -> instead of a dot :)

Upvotes: 1

pdu
pdu

Reputation: 10413

I'd guess that this is not possible since . is used as concatenating operator. Where would we be if everyone could change these things, the language would be very unstable I guess.

So: nope.

Upvotes: 5

Emil Vikström
Emil Vikström

Reputation: 91912

It may be possible to do that by changing the code parser. You will of course need to test this carefully and then recompile PHP. You cannot do this from within PHP, only by rewriting the C source code.

Upvotes: 6

Sarfraz
Sarfraz

Reputation: 382686

No you cant do that, it comes from the core of the language you cant change. AFAIK certain other languages use that too, PHP stood by the convention.

Upvotes: 4

greew
greew

Reputation: 539

Quick answer: No, it's not possible!

Upvotes: 3

ThiefMaster
ThiefMaster

Reputation: 318488

No, that's not possible. Probably even by modifying the language's grammar it would be extremely hard since it could cause ambiguity - $foo.bar() could then either be a method call or concatenating $foo and the return value of bar().

Use a different language if you do not want ->. My suggestion would be python - it's much saner than PHP anyway.

Upvotes: 9

Related Questions