Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question on something I've seen ($this->)

    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.
    Code:
    $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
    Code:
    $today   = date('Y-m-d',strtotime($MyYear . "-" . $MyMonth . "-" . $MyDay));
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    It is used in classes, I'll do you an example now:

    PHP Code:
    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
    Last edited by Mwnciau; 08-15-2006 at 04:36 PM.

  • #3
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 )
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    search on google for some, heres one here

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •