Can someone tell me what "->" does in PHP. I think in C++ there is also such a symbol.
Below is a piece of code from PHP Manual that used "->"
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
Thanks.
Jie Huang
what does -> mean?
Moderator: General Moderators
-> is used in classes.
assigns the string "How nice of you" to the class-variable (or class-property if you like) in the class itself.
"$this" stands for the current instance of the class.
If you would be doing the same thing from outside the class it would look something like this
I am sure you'll find some good object-oriented programming guides if you look with google.
Code: Select all
<?php
$this->readMe="How nice of you";
?>"$this" stands for the current instance of the class.
If you would be doing the same thing from outside the class it would look something like this
Code: Select all
<?php
$readup=new myReadingClass;
$readup->readMe="How nice of you";
?>-
jiehuang001
- Forum Commoner
- Posts: 39
- Joined: Mon May 12, 2003 12:53 pm
Yes PHP's '->' is the same as Java's '.'. Its the member attribute/method selector used on a class instance. PHP also uses the '::' operate to call static functions off a non-instantiated class.
Mini history of -> from C:
The '->' operator comes from C where var->something was converted to (*var).something. Where var is most typically a "struct" aka a 'record' in Pascal, or a "method-less class with all public variables" in Java. Due to C being a pass-by-value languange, pointers were needed to let functions change their arguments. Structs were the major type used for making data structures so you can imagine they are always being passed around and needing to be changed. Typeing (* ). (five characters) all the time got old so someone (classic lazy hacker syndrome) created -> (two characters) as "syntatic sugar". (The parenthesis were needed because '.' had a higher order of precendence than the variable deference.
Mini history of -> from C:
The '->' operator comes from C where var->something was converted to (*var).something. Where var is most typically a "struct" aka a 'record' in Pascal, or a "method-less class with all public variables" in Java. Due to C being a pass-by-value languange, pointers were needed to let functions change their arguments. Structs were the major type used for making data structures so you can imagine they are always being passed around and needing to be changed. Typeing (* ). (five characters) all the time got old so someone (classic lazy hacker syndrome) created -> (two characters) as "syntatic sugar". (The parenthesis were needed because '.' had a higher order of precendence than the variable deference.