PDA

View Full Version : What is $this->?


Guest190829
08-06-2005, 04:46 AM
OOP is giving me a headache. I'm working on vbspace, and I'm just messing around with the space class...is this correct for the most part? It's not the full class just one function



<?php

class space //myspace class: the backbone of the hack.
{
var $type;
var $cache;

public function get_soptions()
{
$options = $db->query_read("SELECT * from " . TABLE_PREFIX . " space_options");

while ($soptions = $db->fetch_array($options))
{
$this->type = $soptions[type];
$this->cache = $soptions[cache];
}
}
}

?>



If not, can you explain what $this-> is? I'm seeing it everywhere, and I think I have it right. It's just a pseudo instance of a the current class. Am I right?

Adrian Schneider
08-06-2005, 04:51 AM
Sadly, yes.

necrotic
08-06-2005, 05:00 AM
Also, make sure $db is globalized somehow (add global $db before calling it for example).

Guest190829
08-06-2005, 05:46 AM
Just another question, when I use the class and make a new instance of it

$new = new space();

Would $new->cache be equal to $this -> cache? I'm guessing it should...

Marco van Herwaarden
08-06-2005, 06:33 AM
Yes, as long as you are inside a function of that class.

Guest190829
08-06-2005, 06:47 AM
How would I get $new->cache to have the same value outside of the function? Make it global?

Marco van Herwaarden
08-06-2005, 06:53 AM
Outside your class you would use $new->cache, inside you use $this->cache

Guest190829
08-06-2005, 06:55 AM
Okay, thanks Marco! :)