allthatcash wrote:I guess what is throwing me off is when I see "$" anything I see that as a variable.
$this is a variable. Actually, strictly speaking, it's a reference to a variable - specifically, the current instance of the class (i.e., an object). Objects, however, are a very different kind of variable than what you're used to.
Most variable types (strings, integers, etc.) have a single value: "apple", 42, etc.
Arrays are a bit more analogous to objects: they can hold multiple values, each of which can be a string, an integer, or even another array.
Objects take things one step further: in addition to containing multiple values (data members, aka properties), they can actually contain functions that act on the object itself. Here's the canonical example:
$fluffy = new Cat( 'Fluffy McFurson' ); // creates a new instance of the class Cat (that is, a Cat object), passing the parameter "Fluffy McFurson" into its constructor function, and stores it in the variable $fluffy
$fluffy->meow(); // outputs "Fluffy McFurson says 'Meow'!"
$fluffy->setAge( 5 ); // outputs "Fluffy McFurson is now 5 years old."
And here's the class definition, which you'd have to include in your PHP file before you could execute the above code:
class Cat {
var $name = ''; // data member
var $age = 0; // data member
// constructor
function Cat( $name ) {
$this->name = $name;
}
// functions that are internal to a class are called "methods" -
// here are a couple of examples
function meow() {
echo $this->name . " says 'meow'!<br />";
}
function setAge( $new_age ) {
$this->age = $new_age;
echo $this->name . ' is now ' . $this->age . ' years old.<br />';
}
}
Think of Cat as a blueprint for a cat (a class), and $fluffy as an actual cat patterned after that blueprint (an object - or, to put it differently, an instance of the Cat class).
Search Google for "object-oriented programming", and find a good introductory tutorial. I'd strongly advise that you become comfortable with ordinary procedural techniques before tackling OOP, though.