Page 1 of 1

-> over .

Posted: Thu Jun 28, 2007 9:59 pm
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

Posted: Thu Jun 28, 2007 10:01 pm
by feyd
No.

Posted: Thu Jun 28, 2007 10:11 pm
by AKA Panama Jack
Nope, using the . would have been counter intuitive.

Posted: Thu Jun 28, 2007 10:12 pm
by Benjamin
Uck, I like the ->

Posted: Thu Jun 28, 2007 11:01 pm
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

Posted: Thu Jun 28, 2007 11:06 pm
by Christopher
... I too would like to add a response worthy of this subject ...

Posted: Thu Jun 28, 2007 11:08 pm
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?

Posted: Fri Jun 29, 2007 12:01 am
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. :)

Posted: Fri Jun 29, 2007 12:25 am
by John Cartwright
Maybe you should try something funnier next time :wink:

Posted: Fri Jun 29, 2007 12:34 am
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. :)

Posted: Fri Jun 29, 2007 12:51 am
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 :)

Posted: Fri Jun 29, 2007 1:07 am
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.

Posted: Fri Jun 29, 2007 1:23 am
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 :)

Posted: Fri Jun 29, 2007 4:12 am
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.

Posted: Fri Jun 29, 2007 1:18 pm
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. :)