Click to See Complete Forum and Search --> : Help Understanding PHP Code


jazzyj99
07-29-2006, 06:42 PM
Can someone please explain how the following variables are being used?

$this->contents = array();
$this->total = 0;
$this->weight = 0;
$this->content_type = false;

Basically I would like to know what the following code does:

$this->

Thanks,

Jeff

NogDog
07-29-2006, 07:27 PM
$this is a special variable within a class definition, referring to "this instantiation of this class". So $this->some_name is either an attribute (i.e.: variable) of this class, or a method (i.e.: function) of this class. For more info: PHP 4 Classes (http://www.php.net/manual/en/language.oop.php) or PHP 5 Classes (http://www.php.net/manual/en/language.oop5.php).

patenaudemat
07-29-2006, 07:27 PM
$this->methodOrProperty references a method or property of the current instance of an object in PHP.

PHP: Classes and Objects (PHP 4) (http://php.net/oop)
PHP: Classes and Objects (PHP 5) (http://php.net/oop5)

In all of the lines of code, contents, total, weight, and content_type are all properties of an object. $this is a reference to the current instance of an object being operated upon.

To determine what type of object you're working in, look for the opening line that says something like...


class ClassName {


Hope it helps!

-Matt

PS, NogDog, apparently we think alike :p I started typing mine before yours was posted.