Jump to content


$this


1 reply to this topic

#1 dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 13 March 2006 - 05:42 PM

Okay this has been bugging me for a while. in php functions and stuff there are stuff like this:

<?php
function name($var)
{
$this->blah($var)
}
function other($get)
{
return $this->blah
}
?>

So first What does "$this" Mean and do, also in the statment up above I just kind of typed out as far as I understand the function other would give the output as blah($var) in the name function.

dEcade

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 March 2006 - 11:22 PM

OOP
http://php.net/manua...ef.classobj.php

$this is only used in objects, not functions. I just looks like it's used in functions because you define functions within classes. The examples php.net has aren't the best to learn from (trust me, i tried), but they do get some of the basics across.

Note: this also exists in JavaScript (and many other programming languages). In JS it referes to the current object or element that is calling something, i.e.
<table>
<tr>
<td onMouseOver="changeBG(this, 1);" onMouseOut="changeBG(this, 0);">changeBG is an imaginary function that would highlight the table cell</td>
</tr>
</table>
Thats at least the best example i can use to get across what $this is used for. I also remember reading a better real life example of how OOP is used, i'll see if i can find it later.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users