-> over .

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

-> over .

Post by alex.barylski »

Personally I would prefered PHP gone the way of JavaScript and used the '.' as the dereference operator. The obvious reason as to why they didn't is likely that it's already an operator, the concatenation operator.

Maybe they should have used + for concatenation and . for dereferencing...

I find myself constantly mucking up the characters -> :P

Bah...end of rant...

Petition anyone? :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

No.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Nope, using the . would have been counter intuitive.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Uck, I like the ->
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

AKA Panama Jack wrote:Nope, using the . would have been counter intuitive.
Counter-intuitive? How so?

More characters = conter-intuitive... :P

astions: Each to there own I guess...for historical purposes it makes sense as the '.' operator is just a member access operator and the '->' is actually dereferencing the pointer, which (from what I understand) PHP is actually doing...so for that reason it makes sense but in a language where there is no distinction between the two, having to punch out '->' is a PITA :P

Switching from JavaScript to PHP to C++ doesn't help either :P
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

... I too would like to add a response worthy of this subject ...
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Considering it would likely take more time to process your code into operations when making the same operator doing multiple things in context, I think the sacrifice of some extra typing is perfectly acceptable. Java and C have the luxury of a compilation, Javascript has the luxury of being a light load for a client, but a server parsing untold numbers of files at any given time, well, you get the idea, right?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

feyd wrote:Considering it would likely take more time to process your code into operations when making the same operator doing multiple things in context, I think the sacrifice of some extra typing is perfectly acceptable. Java and C have the luxury of a compilation, Javascript has the luxury of being a light load for a client, but a server parsing untold numbers of files at any given time, well, you get the idea, right?
Perhaps, but PHP supports various CRT style functions, which should be used over concatenation operator anyways (sprintf and company) so they could do away with the '.' operator as concatenation and use it for member access instead.

Of course, you realize that is a joke? Apparently I have to make this clear because many of you seem to think I am serious or that unpractical. :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Maybe you should try something funnier next time :wink:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Hockey wrote:Perhaps, but PHP supports various CRT style functions, which should be used over concatenation operator anyways (sprintf and company) so they could do away with the '.' operator as concatenation and use it for member access instead.
sprintf et al are completely impractical for most string operations in this language. Why do you think C++ added the String class and Java natively has a String class which is always included?
Hockey wrote:Of course, you realize that is a joke? Apparently I have to make this clear because many of you seem to think I am serious or that unpractical. :)
How could we possibly know you are joking? Many-a-time have you posted of similar outlandish, neigh impossible, ideas and were totally serious. If you need your memory jogged, how about the time you wanted to create and thereby impose a coding style. I'd say that's pretty similar in the "it won't happen, ever" scenario that you're fairly well known for. Whether that's a bad or good thing, hard to say. :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

feyd wrote:
Hockey wrote:Perhaps, but PHP supports various CRT style functions, which should be used over concatenation operator anyways (sprintf and company) so they could do away with the '.' operator as concatenation and use it for member access instead.
sprintf et al are completely impractical for most string operations in this language. Why do you think C++ added the String class and Java natively has a String class which is always included?
Hockey wrote:Of course, you realize that is a joke? Apparently I have to make this clear because many of you seem to think I am serious or that unpractical. :)
How could we possibly know you are joking? Many-a-time have you posted of similar outlandish, neigh impossible, ideas and were totally serious. If you need your memory jogged, how about the time you wanted to create and thereby impose a coding style. I'd say that's pretty similar in the "it won't happen, ever" scenario that you're fairly well known for. Whether that's a bad or good thing, hard to say. :)
I'm curious why they are impractical? I personally prefer sprintf over string concatenation operators. For starters I imagine it's faster than relying on:

a) String interpolation
b) The parser/interpreter to join strings togather

Just because C++ (more specifically MFC) has a CString with the '+' operator overloaded doesn't mean it's practical or useful. In fact if you look into the source of MFC and the usage of CString I'm willing to bet most uses avoid that and instead opt for sprintf equivelants. Sure there are times perhaps when:

Code: Select all

$str = 'this'.'is'.'a'.'string';
Might be more "handy" than:

Code: Select all

$str = sprintf('%s%s%s%s', 'this', 'is', 'a', 'string');
But I wouldn't consider it more practical. If anything its less practical, because anyone coming from C/C++ would immediately recognize and feel comfortable with sprintf and possibly lost at the sight of a '.' acting as concatenation. That, and using sprintf is far more flexible and powerful and should be used when appropriate instead of hacking togather strings using operaters and PHP's string interpolation...

Whatever man, personal choice, thats fine. But to claim one is more or less practical without providing practical examples indicating where one excels over the other, is like calling a Goose a Duck when it's actually a Swan. :P

As for Java. Because *everything* is an object. You mean to tell me Java string classes don't have a Format or Sprintf or similar method as part of that class API? It relies solely on an operator to provide concatenation?

JCart I had a good one, but it slipped. :P

Edit: strict coding styles actually do offer practical advantages. Self describing code, consistency and not to mention variables, etc which follow convention make for easy times in developing tools to auto-extract details about a software system.

Top quality software goes far beyond MVC or MVP and design patterns and/or object practices and principles. I like having applications with near perfect coding styles, comments, architecture, file structure, core functionality, documentation, etc, etc, etc...maybe thats just me. :D

Cheers :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  • It's interesting to see you tout the use of sprintf() when you are often one to go for less keystrokes and "easy." ;)
  • I wasn't referring to MFC and CString, I was talking about the STL's String.
  • Java has sprintf in the String class, but to use it for concatenation is just normally not done; it's silly when the plus operator they've provided for it is right at hand.
I fully agree sprintf() has it's uses, but straight concatenation is just not one of them in PHP.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

feyd wrote:
  • It's interesting to see you tout the use of sprintf() when you are often one to go for less keystrokes and "easy." ;)
  • I wasn't referring to MFC and CString, I was talking about the STL's String.
  • Java has sprintf in the String class, but to use it for concatenation is just normally not done; it's silly when the plus operator they've provided for it is right at hand.
I fully agree sprintf() has it's uses, but straight concatenation is just not one of them in PHP.
Perhaps for simple concatenation.

Code: Select all

$firstname.$lastname
Unless of course you throw a space in there:

Code: Select all

$fullname = $firstname.' '.$lastname;
I would opt for sprintf

Code: Select all

$fullname = sprintf('%s %s', $firstname, $lastname);
Besides, sprintf comes in *real* handy when your dealing with multi-lingual applications or configuration is important. ;)

Code: Select all

define('FULLNAME', '%s, %s'); // Format a name nicely - not adhoc hackish like concatenation.

$fullname = sprintf(FULLNAME, $firstname, $lastname);
Locatization using concatenation:

Code: Select all

define('SEPARATOR', ','); 

$fullname = $firstname.SEPARATOR.$lastname;
Personally I just prefer sprintf for most string ops...perhaps old habits die hard but I'll take the speed and flexibility over quick and dirty in this case. :)

I am curious though, when is it most beneficial to use concatenation operator when joining strings, in your opinion? Perhaps I can add that to current development habits to improve myself.

Edit: I should note that I do indeed use concat's but only when I'm in a quick and dirty mood like debugging code and I need two or more args printed nicely side by side.

Code: Select all

echo $name.' - '.$age.'<br>';
In that instance, yup...I couldn't live without concat's

Cheers :)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, you've mentioned C++ quite a few times, so I'll use that to defend the dereferencer.
In C++, the -> operator is used to access members of references. PHP 5 automatically uses object references. Just though I should trow that out there. :P

Really though, PHP has a bit more required syntax to speed things up. Why else would we have to type a dollar sign at the beginning of every single variable? If your quarrel is accidentally typing PHP code in other languages, you've still got that dollar sign.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

superdezign wrote:In C++, the -> operator is used to access members of references
I think I would define it as: Used to dereference object member variables.
superdezign wrote:PHP 5 automatically uses object references. Just though I should trow that out there
Yup, but PHP 4 doesn't, so what? You still use the '->' operator. :P

I mention the historical/technical reasons why PHP likelu uses '->' over '.' then it turned into a disscussion as to why '.' was or wasn't good practice. Personally, I'm not a big fan. Until someone can convince me otherwise, I'll remain stubborn in my stance that using language 'hacks' are just that. :)

Have any good examples of when using concat's is simply better than using sprintf, cause I've mention a few. :)
Post Reply