Help for a Newbie

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
webKP
Forum Newbie
Posts: 3
Joined: Tue Sep 03, 2002 11:33 pm

Help for a Newbie

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please read i.e. Chapter 14. Classes and Objects or google for "object classes php" ;)
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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,
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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


?>
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Sorry about that... I'll be more careful.
webKP
Forum Newbie
Posts: 3
Joined: Tue Sep 03, 2002 11:33 pm

Thanks everyone! All a big help! -eom-

Post by webKP »

no message
Post Reply