Hello. I am trying to decipher some PHP code. I am unsure if "$this" is a commonly used variable and what "->" is. Another variable I am seeing a lot of is "$schart." Both lines of code are below, however, they do not follow one another in the script itself. I know this is not a lot of info, but any insight would be appreciated.
$this->userinfo=$userinfo;
$schart->createBasket($HTTP_POST_VARS);
Thanks in advance
kp
Help for a Newbie
Moderator: General Moderators
please read i.e. Chapter 14. Classes and Objects or google for "object classes php" 
- gite_ashish
- Forum Contributor
- Posts: 118
- Joined: Sat Aug 31, 2002 11:38 am
- Location: India
hi,
'$this' keyword refers to the self, it is used inside the class definition to refer to the self.
'->' operator is ued to access variable (attribute) OR function (method) of the class by the class instance variable.
See PHP OOPs manual for details:
http://www.php.net/manual/en/language.oop.php
regards,
'$this' keyword refers to the self, it is used inside the class definition to refer to the self.
'->' operator is ued to access variable (attribute) OR function (method) of the class by the class instance variable.
See PHP OOPs manual for details:
http://www.php.net/manual/en/language.oop.php
regards,
Example
to access this class you need to have the following code
You can then access the function like this
To access the varibale defined in the class you use "$this".
$this is same as $example1, if you get what I mean.
So when you are defining class and you are trying to access one of the function or variable you user this format
Code: Select all
<?php
class example {
var option;
function print_msg($msg)
{
if($this->option == 0) {
echo $msg;
}
}
}
?>Code: Select all
<?php
$example1 = new example();
?>Code: Select all
<?php
$Example1->print_msg("Hello World!");
?>$this is same as $example1, if you get what I mean.
So when you are defining class and you are trying to access one of the function or variable you user this format
Code: Select all
<?php
$this->print_msg("Hello World!");
or echo $this->option
?>Although be careful with the case of the variablesto access this class you need to have the following code
PHP:
<?php
$example1 = new example();
?>
You can then access the function like this
PHP:
<?php
$Example1->print_msg("Hello World!");
?>
$example1 is not the same as $Example1
$this is only available within the class itself, I don't think Takuma makes this clear with that example. You can't just write $this anywhere in your php page and it knows what object you are refering to.To access the varibale defined in the class you use "$this".
$this is same as $example1, if you get what I mean.
So when you are defining class and you are trying to access one of the function or variable you user this format
PHP:
<?php
$this->print_msg("Hello World!");
or echo $this->option
?>
Thanks everyone! All a big help! -eom-
no message