(did you know if you search for "$this" on google it sees it as "this"
What is $this?
Moderator: General Moderators
- Vash the Stampede
- Forum Newbie
- Posts: 7
- Joined: Tue Mar 30, 2004 6:15 pm
What is $this?
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"
)
(did you know if you search for "$this" on google it sees it as "this"
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
$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.
Code: Select all
<?php
$this = "hello world";
echo $this;
// this would out 'hello world', it would echo the variable $this, if that helps.
?>$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.
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
Code: Select all
<?php
class Example
{
var $name;
var $password;
function Import()
{
//Do some database stuff
$this ->name = $row['Name'];
$this ->password = $row['Password'];
}
}
?>dont know if that makes sense do you, so please reply
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.
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.
- Vash the Stampede
- Forum Newbie
- Posts: 7
- Joined: Tue Mar 30, 2004 6:15 pm
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.
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.
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
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.
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
on a sidenot here: some languages use 'self'.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.
most use this though.