Jump to content
  • Advertisement
Sign in to follow this  
sofsenint

[web] PHP and -&gt

This topic is 4872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a very newbie question about PHP. What exactly does -> do? I know that in C plus plus, it's for dereferencing a pointer and accessing a member of a class, but from code examples I've seen, I don't think this is the case in PHP. Is it just to access any member of a class, without dereferencing it? This seems like something that should be easy for me to find using Google or the PHP manual, but it's been hard to search for ->. Thanks for your help!

Share this post


Link to post
Share on other sites
Advertisement
-> is used in php to refer to an object's member variable or call a member function. It's like . in c++.

Of course they couldn't use . because it was already used for string concatenation.

Mark

Share this post


Link to post
Share on other sites
here this is a example, hope this is useful

[source code="php"]
<?
class classTest
{
var $test;
}

$something = new classTest;
$something->test = "Hello";
echo $something->test;
?>

Share this post


Link to post
Share on other sites
Thank you both for your help! I really appreciate it. I hadn't made that connection with the concatenation operator, but now that I think about it, it does make sense. Thanks again!

Share this post


Link to post
Share on other sites
Quote:
Original post by markr
-> is used in php to refer to an object's member variable or call a member function. It's like . in c++.

Of course they couldn't use . because it was already used for string concatenation.

Mark


They most certainly could have, but they chose not to. I'm obviously not privy to what the Zend guys were thinking at the time, but I wager that the total lack of re-use of operators has a lot to do with the fact that all scripts have to be compiled at run time. I suspect that we won't see any real operator overloading features in the language for quite some time as well, as a result.

Share this post


Link to post
Share on other sites
The semantics of PHP do not mean that the type of a variable has to be known at compile time. Therefore, overloading . to do member resolution would not work, because something like:


$x . setFoo(42);


Would compile into a nonsensical statement if . was string concatenation (and setFoo() did not exist as a plain function).

So it would be ambiguous at compile-time. This would be stupid.

The compiler would then have to compile two different bits of code, one for if $x was an object, another for if it wasn't. That would be silly.

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!