What is $this?

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
User avatar
Vash the Stampede
Forum Newbie
Posts: 7
Joined: Tue Mar 30, 2004 6:15 pm

What is $this?

Post by Vash the Stampede »

Ok, I am learning PHP on my own, reading tutorials, forums, I bought 2 books, and just general stumbleing around PHP scripts. I got one question I can't quite answer. What is "$this"? I think I know what it is. I see it used alot in classes, but I am just not sure if I am useing it right. I have been reading the PHP man. and haven't found it just yet. I know it has to be a simple thing I am just not getting. Thank you.

(did you know if you search for "$this" on google it sees it as "this" :) )
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

$this is used to call properties (variables) and methods (functions) in an object, within the object.

Code: Select all

<?
class some_object {
var $somevar;
var $somevar2;

function some_function() {
    // some code
    // set the variable within the object
    $this->somevar = 4;
}

// call the function within the object
$this->some_function();
}?>
Last edited by andre_c on Tue Mar 30, 2004 6:20 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

Code: Select all

<?php
$this = "hello world";
echo $this;

// this would out 'hello world', it would echo the variable $this, if that helps.

?>
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

$this is used to refer to the original class. for example, lets say i have a function that imports stuff from my database and stores it into values of the class.

Code: Select all

<?php
class Example
{
  var $name;
  var $password;

  function Import()
  {
    //Do some database stuff
    $this ->name = $row['Name'];
    $this ->password = $row['Password'];
  }

}

?>
In this example, $this points to the class Example, and since the function is a part of Example, you dont need to declare an object as Example

dont know if that makes sense do you, so please reply :D
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

damn andre, you beat me to it!

and tim, $this isnt just some normal variable :P
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

At first glance I thought he was talking about variables too. :)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

heh

If you've worked enough with OOP, you should have known $this could only have been one thing. ;)

Vash: if you are just starting out with PHP, you shouldn't have to worry about OOP for a long time to come.

There is many more things that need to be overcome before tackling object oriented programming.

BTW, welcome to the community.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

I need to read the pull thread before i jump to conclusions.

Sorry :cry:
User avatar
Vash the Stampede
Forum Newbie
Posts: 7
Joined: Tue Mar 30, 2004 6:15 pm

Post by Vash the Stampede »

Cool, thanks alot guys. I was close when I guessed what I thought it was. The reasion I asked is the book I am reading had me makeing a class but said "Classes and Objects are beyond the scope of this book." It didn't even explain a simple logic in it eather, it just said "type this in". :(

Sami, you are right. I went to college for computer programing and it was a waste. 3 semesters of COBOL & CICS, 1 semester of VB 6.0 and 6 weeks of C++. Not one second was spent on OOP. I can't beleave all they tought was how to fix old COBOL programs. :x Oh well enough complaining. Thanks every one.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

heh i had a very abstract comp sci prof. he couldnt run windows to save his life, but he knew his way around lanquages and taught us so much theory it would take me a while to be able to code after his classes because i would think about whatever language it was all the way down deep and start getting confused. But we used java for everything and so I know OOP like I know english or the back of my hand or something.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I believe if you pickup a good book dedicated to PHP & OOP, add in a great message board like this, and some web tutorials/articles, you will be better off than taking an expensive course on the subject.


:)
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Yea and i think it will be good to learn it as you learn the language otherwise it may be difficult to change habits when you start learning it
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

I spent two years in college.
Everything I know about programming:
1% Learned in College
99 % Learned from google, php.net, forums, books, tutorials, (on my own)
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

andre_c wrote:I spent two years in college.
Everything I know about programming:
1% Learned in College
99 % Learned from google, php.net, forums, books, tutorials, (on my own)
^^^same here :P
Zay
Forum Newbie
Posts: 22
Joined: Tue Mar 23, 2004 3:42 pm

Post by Zay »

Sami wrote:heh

If you've worked enough with OOP, you should have known $this could only have been one thing. ;)

Vash: if you are just starting out with PHP, you shouldn't have to worry about OOP for a long time to come.

There is many more things that need to be overcome before tackling object oriented programming.

BTW, welcome to the community.
on a sidenot here: some languages use 'self'.
most use this though.
Post Reply