Page 1 of 1
Help for a Newbie
Posted: Tue Sep 03, 2002 11:33 pm
by webKP
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
Posted: Tue Sep 03, 2002 11:53 pm
by volka
please read i.e.
Chapter 14. Classes and Objects or google for "object classes php"

Posted: Tue Sep 03, 2002 11:54 pm
by gite_ashish
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,
Posted: Wed Sep 04, 2002 1:27 am
by Takuma
Example
Code: Select all
<?php
class example {
var option;
function print_msg($msg)
{
if($this->option == 0) {
echo $msg;
}
}
}
?>
to access this class you need to have the following code
Code: Select all
<?php
$example1 = new example();
?>
You can then access the function like this
Code: Select all
<?php
$Example1->print_msg("Hello World!");
?>
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
$this->print_msg("Hello World!");
or echo $this->option
?>
Posted: Wed Sep 04, 2002 3:28 am
by mikeq
to 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!");
?>
Although be careful with the case of the variables
$example1 is not the same as $Example1
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
?>
$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.
Posted: Wed Sep 04, 2002 9:38 am
by Takuma
Sorry about that... I'll be more careful.
Thanks everyone! All a big help! -eom-
Posted: Fri Sep 06, 2002 12:20 am
by webKP
no message