View Full Version : Question on something I've seen ($this->)

08-15-2006, 05:14 PM
I've seen code like this before in OS code I've looked at, and on other sites. I'm a bit confused as to what "$this->" is or does.

$today = date('Y-m-d',strtotime($this->year . "-" . $this->month . "-" . $day)); I'm guessing it's some type of variable, but why not just a standard one? ie

$today = date('Y-m-d',strtotime($MyYear . "-" . $MyMonth . "-" . $MyDay));

08-15-2006, 05:30 PM
It is used in classes, I'll do you an example now:

class hi {
var $name;
var $last; // variables inside the class
function display(){ // function inside the class
echo $this->name . ' ' . $this->last;
$user = new hi; // Create the class (you can create more than 1)
// Normally to access the variables we would have to do this:
echo $user->name; // echos the variable name in the class $user
// But we can just call the function made inside the class:
$user->display(); // executes the function display() in class $user
// If we have a function inside a class it doesn't actually know the variable you have used for the class ($user)
// so instead it just uses $this which accesses the variable inside the class the function it is in

hope thats clear

08-15-2006, 05:52 PM
Thanks Mwnciau, Makes sense and the light is starting to shine.

Ok any one have any good reference or tutorial links for Using/Doing/Writing classes for the beginner, for some reason the concept of classes just isnt sinking in.

I guess I'm looking for a Beginner Classes Class (ok ok bad pun)

(Heck I'll even get a book or two if you want to recommend one of those :p)

08-15-2006, 06:24 PM
search on google for some, heres one here (http://www.phpfreaks.com/tutorials/48/0.php)

08-15-2006, 07:12 PM
Classes are an entire problem solving paradigm.

What you're most interested, very specifically, is object-oriented programming. Classes are one style of OOP. Prototypes (as in ECMA/JavaScript) are a different style of OOP.

You want to look into some object-oriented programming tutorials and learn about the whole thing. Classes are the most popular way of implementing objects, so you'll most likely find class-based oop tutorials relatively easily.

Or, take a course at a local community college on it. It's worth it sometimes to have it explained to you.