Page 1 of 1
What is ->
Posted: Tue Jan 30, 2007 8:02 pm
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

Posted: Tue Jan 30, 2007 8:04 pm
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.
Thank you!
Posted: Tue Jan 30, 2007 8:11 pm
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

Posted: Tue Jan 30, 2007 8:16 pm
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
Nice
Posted: Tue Jan 30, 2007 8:19 pm
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?
Posted: Tue Jan 30, 2007 8:21 pm
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
Posted: Tue Jan 30, 2007 8:43 pm
by feyd
Posted: Tue Jan 30, 2007 8:52 pm
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
Posted: Tue Jan 30, 2007 10:04 pm
by Z3RO21
I hope I am not being a total retard but
ole why do you use:
instead of
?
Posted: Tue Jan 30, 2007 10:33 pm
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.
Posted: Tue Jan 30, 2007 11:23 pm
by Kieran Huggins