Php question: $this and ->

Permabanned
Joined
3 Jul 2008
Posts
3,762
Location
My fabulous ship
Hey guys noob question here and I was never actually taught this in university so have never used these, but I have to adopt some other guys coding and Im not entirely sure what he's done here.

So can someone give me a definition of what $this and "->" does? because typing them in google (even with filtering) brings nothing related.

Its being used here:

<h2><span>Band/artist</span></h2>
<div class="sidebaritem">
<form id="sidebarsearch" action="/search/dosearch/" method="post">
<input type="text" name="searchstring" id="searchstring" value="<?=$sidebarsearchstring?>">
<input type="submit" class="submit" value="Search">
</form>
</div>

<?php if ($this->session->userdata('logged_in') && ($this->session->userdata('usertype') != 3)) : ?>

<?php if ($this->session->userdata('userid'))
{
$band_id = $this->session->userdata('bandid');
$this->db->where('id', $band_id);
$query = $this->db->get('band');
$this_band = $query->row();
$band_perma = $this_band->permalink;
}

?>

<h2><span>My GBOB</span></h2>

<div class="sidebaritem mygbob">
<ul class="nav">
<?php
$this->db->where('pagetype', 'band');
$this->db->where('active', 'yes');
$this->db->order_by('order', 'asc');
$query = $this->db->get('gbob_pages');
$band_pages = $query->result();

$nav = '';
foreach($band_pages as $p)
{
$nav .= '<li class="';
if ($p->protected == 'yes')
{
$nav .= 'edit';
if ($p->link == $this_page->link)
{
$nav .= ' ';
}
}
if ($p->link == $this_page->link && (!empty($this_page->link) || $this_page->order == 1) && $page == 'band' && $band->id == $this->session->userdata('bandid'))
{
$nav .= 'active';
}
$nav .= '"><a href="/' . $band_perma . '/';
if (!empty($p->link))
{
$nav .= $p->link . '/';
}
$nav .= '">' . $p->title . '</a></li>';
}
$nav = str_replace('li class=""', 'li', $nav);
print $nav;
?>
</ul>

<p class="button"><a href="/logout/">Log out</a></p>
</div>
<?php else : ?>

<h2><span>Join GBOB</span></h2>
<div class="sidebaritem">
<p class="intro">Sign up for free</p>
<p class="button"><a href="/register/">GO!</a></p>
</div>

<h2><span>Log in</span></h2>
<div class="sidebaritem">
<form id="loginform" method="post" action="/login/submit">
<fieldset>
<label for="l_username">E-mail</label>
<input type="text" name="username" id="l_username" value="">
<label for="l_password">Password</label>
<input type="password" name="password" id="l_password" value="">
<input type="submit" class="submit" value="Log in">
<p><a href="/password/">&rsaquo; Forgotten password?</a></p>
<p id="loginfeedback"></p>
</fieldset>
</form>
</div>

<?php endif; ?>

<?php
$data = array();
$this->load->view('includes/sidebar_sponsor', $data);
?>

Also can anyone recommend any good further reading on php books
thanks
 
-> is an arrow operator and $this is a this pointer. An arrow operator is used to access members of an object eg $this_band->permalink gets permalink from the object $this_band. $this is just a way of accessing members of the class from within the class eg.

Code:
class a
{
public $hello;

public function __construct(){

$this->$hello = 'hello';
//Note $hello = 'hello'; will not update the class member $hello.
}
 
Last edited:
-> is an arrow operator and $this is a this pointer. An arrow operator is used to access members of an object eg $this_band->permalink gets permalink from the object $this_band. $this is just a way of accessing members of the class from within the class eg.

Code:
class a
{
public $hello;

public function __construct(){

$this->$hello = 'hello';
//Note $hello = 'hello'; will not update the class member $hello.
}
Couple of nitpicks.. $this is the pointer for 'current object' and it's accessing the object member, not class member. Subtle difference that can have a massive impact. :)
 
Couple of nitpicks.. $this is the pointer for 'current object' and it's accessing the object member, not class member. Subtle difference that can have a massive impact. :)

Yes you are correct, its a pointer to an instantiated class member ie object member. I also missed out an ending brace of my class example :(
 
Last edited:
Looks like a (badly written) codeigniter view page.

You might find this useful

hehe you think this is bad? you dont wanna see the site! I gotta go through all of his code - of which he has totally over complicated hidden, seperated code - argh its a nightmare. He's taken code from other work he did and pasted in so some common variables do one thing on one page and then completely different on another - and his database!?!?!?! You'd swear he's using a rand() equation to generate names for his tables! and theres no annotations or notes to refer to!

yeah he's using code igniter, Im trying to avoid it but *sigh* I guess you cant prevent the inevitable and I suppose I'd get a better perspective if I take a look at it. >.<

thanks for the documentation though
 
Back
Top Bottom