What is ->

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ykarmi
Forum Commoner
Posts: 35
Joined: Mon Oct 30, 2006 4:45 pm

What is ->

Post by ykarmi »

Hey guys, I tried googling it, but google wouldn't accept the string "->" so i couldn't find out without asking.
What is the sign -> in php?
For example:
$logger->_log('You are not logged in; return to index.php');
What does the arrow do?
Thanks!
Yuval :D
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You could have searched for "php operator arrow" or something.

The -> "arrow" is used in classes. You know.. OOP. :-p

Edit: Well not IN classes. To access class variables and such. You know, when you're outside the class. Yeah. Don't want you confused.
ykarmi
Forum Commoner
Posts: 35
Joined: Mon Oct 30, 2006 4:45 pm

Thank you!

Post by ykarmi »

Thank you!
Didn't think of googling that, "operator arrow" lol - i wasn't sure that was the name, but thinking about it, it was worth the try...

Anyways, why would someone write $this->_classVar
what is the $this - is it preset by PHP or something someone coded?
Thank you!
Yuval :D
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

$this is a variable. Hence the lovely dollar sign, showing people that PHP should be equivalent to money. Yes.
echo "$".number_format($myCash, 2, '.', ',');

That number could be big... I'm off track.

But yeah, $this is representative of a class variable when used in the statement $this->variable;
$this would have been declared a class in such a way that classes are declared in all OO languages... $this = new classConstructor()

OR it's in the class. "$this->" is like "this."... I'm sure you shouldn't have a reason to name a variable "$this".... Silly me

Well, except C++.. but online, C++ just served as a way to make me learn PHP 15 times faster
Last edited by superdezign on Tue Jan 30, 2007 8:20 pm, edited 1 time in total.
ykarmi
Forum Commoner
Posts: 35
Joined: Mon Oct 30, 2006 4:45 pm

Nice

Post by ykarmi »

Interesting reply! =)
lol, so this is defined by the class as new classConstructor(), then it is being used to access the class?
I think i'm confused, why would someone want to use $this?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Lol I just edited my post saying exactly that.

I don't know what I was thinking.

Maybe I should have wrote "Edit:"? :-p
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Quick example (PHP4):

Code: Select all

class Foo
{
    var $total = 0;
    function add($num) {
        $this->total += $num;
    }
}
$a = &new Foo();
$a->add(5); $a->add(6);
echo $a->total; // 11
$b = &new Foo();
$b->add(3);
echo $b->total; // 3
echo $a->total; // 11
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

I hope I am not being a total retard but ole why do you use:

Code: Select all

$a = &new Foo();
instead of

Code: Select all

$a = new Foo();
?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

In PHP 4, the latter results in a copy being generated, versus the original object. For PHP 5, the latter is the one to use.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Post Reply