Page 1 of 1
What is $this?
Posted: Tue Mar 30, 2004 6:15 pm
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"

)
Posted: Tue Mar 30, 2004 6:17 pm
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();
}?>
Posted: Tue Mar 30, 2004 6:20 pm
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.
?>
Posted: Tue Mar 30, 2004 6:20 pm
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

Posted: Tue Mar 30, 2004 6:21 pm
by Deemo
damn andre, you beat me to it!
and tim, $this isnt just some normal variable

Posted: Tue Mar 30, 2004 6:22 pm
by andre_c
At first glance I thought he was talking about variables too.

Posted: Tue Mar 30, 2004 7:04 pm
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.
Posted: Tue Mar 30, 2004 7:35 pm
by tim
I need to read the pull thread before i jump to conclusions.
Sorry

Posted: Wed Mar 31, 2004 12:41 pm
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.

Oh well enough complaining. Thanks every one.
Posted: Wed Mar 31, 2004 12:49 pm
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.
Posted: Wed Mar 31, 2004 1:13 pm
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.

Posted: Wed Mar 31, 2004 1:20 pm
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
Posted: Wed Mar 31, 2004 1:20 pm
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)
Posted: Wed Mar 31, 2004 1:50 pm
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

Posted: Wed Mar 31, 2004 2:55 pm
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.