Jump to content


Photo

how do you say "->" ?


  • Please log in to reply
30 replies to this topic

#21 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 24 February 2007 - 11:23 AM

Personally I dont use ->

but thats because I am RUBBISH and scared of classes of my own......

so using Roopurt's analogy

$foo->bar(); ==> foo does bar


for me it should be

$I!->classes(); ==> I do not do classes


you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#22 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 24 February 2007 - 02:14 PM

I can't say why the PHP developers chose the -> operator over the dot operator, but I disagree with jesi's comment.

It's not terribly difficult to have one operator that performs different operations depending on the context in which it's used.  PHP already does it with LPAREN, which denotes function arguments or order of operations.  C++ uses context to allow function overloading.  Javascript uses the + operator for both string concatenation and arithmetic addition.


But then we wouldn't be able to:

$obj->{$foo.$bar}();
${$foo.$bar}->foobar;
$obj->${$foo.'bar'};

String concatenation and arithmetic addition are things that can't be combined in such a way.

#23 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 24 February 2007 - 02:31 PM

Of course if the php string concatenation operator where to change to the plus sign:

$obj.{$foo+$bar}();
${$foo+$bar}.foobar;
$obj.${$foo+'bar'};

::)

#24 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 24 February 2007 - 05:06 PM

I personally say "calls"... $dog->bark();

So in my head, I say "dog calls bark".  When I'm referencing a class variable "$dog->poop = yellow" I just simply say dog poop equals yellow... I personally think it differs depending on whether you're referencing a variable or a method/function.

Info: PHP Manual


#25 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 24 February 2007 - 06:58 PM

But then we wouldn't be able to:

$obj->{$foo.$bar}();
${$foo.$bar}->foobar;
$obj->${$foo.'bar'};


Not so.

<?php
$obj->{$foo.$bar}();
// Would become...
$obj.{$foo.$bar}();
?>
If $foo is an object, we mean the property of $foo indicated by the variable $bar.  If $foo is not an object, we mean string concatenation, since there is no LPAREN after $bar.
If $obj is an object, we mean to call the method of $obj named after the concatenation of the variables $foo and $bar.  If $obj is not an object, we mean to concatenate the contents of $obj with the results of the function named after the concatenation of the variables $foo and $bar.

<?php
${$foo.$bar}->foobar;
// Would become
${$foo.$bar}.foobar;
?>
If $foo is an object, we mean the property of $foo indicated by the contents of $bar.  If $foo is not an object, we mean string concatenation of $foo and $bar since $bar has no trailing LPAREN.
If the variable named for the result of the curly bracket expression is itself an object, we mean the foobar property of that object, otherwise you have a syntax error (expected T_DOLLAR_SIGN before 'foobar').

<?php
$obj->${$foo.'bar'};
// Would become
$obj.${$foo.'bar'};
?>
If $foo is an object, it is converted to the string 'object' and concatenated with 'bar'.  This is done because 'bar' is not a legal identifier.  Otherwise the contents of $foo are concatenated with 'bar'.
If $obj is an object, we mean the property contained within the variable named by the curly bracket expression.  If $obj is not an object, we mean string concatenation.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#26 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 24 February 2007 - 08:11 PM

I guess that COULD be implemented. It would bring a lot of complex changes to the php core me thinks though..

Not to mention it wouldn't exactly improve readabilty of code...  ;)

#27 Nameless12

Nameless12
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 01 March 2007 - 08:57 PM

"->"  is used in c when you have a pointer to a structure and as we all know php is made in c. In c "->" means "points at"

#28 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 01 March 2007 - 09:02 PM

Yes, that's what it means and does in C, but it doesn't do the same thing in PHP.
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#29 Nameless12

Nameless12
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 02 March 2007 - 01:06 AM

You are right it does not do the same thing because php is not c. But pointers to structures and instances are so alike that this is the reason cpp and php and perl and a few others chose to use the points at symbol and that is why i said what I did but after saying this i wonder if php copied perl, c or cpp most probably all 3. I am not quite sure, It might be time for me to learn more of the php internals I have put it off for so long.




#30 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 02 March 2007 - 02:29 AM

Maybe the original person(s) that implemented objects in PHP didn't want to deal with the added complexity of dealing with the problems myself and 448191 discussed above; or they didn't know how to deal with them.

Either way, they chose -> and the bottom line is no one has bothered to go back and implement a dot operator that does the same thing.  And why would anyone want to?  There's no practical reason in this case to go back and implement a second operator that does the exact same thing as one that already exists.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#31 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 02 March 2007 - 05:44 PM

I'm guessing they used -> instead of . because they implemented . as concatenator before oop was implemented, simple as that. 
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users