Jump to content

Recommended Posts

This may have been answered already, but try searching for $this.  :P

Anyhoo, I would love to find out definitively, without any doubt, what the heck it means?

How is it used? (If you leave a code example, please use anything but FOO!!  ::) )

And, what is $this->output.= '</table>'; doiing? (Just an example)

I know that it is somehow performing "echo" or "print" but what is -> doing and where is all this (pardon the pun) coming from?

 

I'm a self taught PHP muddler, so the whole function/method thing is a vague concept to me. I get the jist, but not anywhere near a commanding understanding. I'm looking for the dumb-down explanation. :)

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/36684-end-my-this-pain/
Share on other sites

This (no pun intended) is based on Object Oriented standards. When you've written a class $this will literally mean this instance of this object. Example:

 

<?php
class A{
public $var1 = "";

public function speak(){
echo $this->var1;
}
}

$a = new A();
$a->var1 = "hello\n";
$a->speak();

$b = new A();
$b->var1 = "world\n";
$b->speak();
$a->speak();
?>

 

This will output:

hello
world
hello

Link to comment
https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174906
Share on other sites

Wow. That means I'm about to get a bigger headache.

I am playing with a 200 line page and it's all for a class with about way-too-many $thises (yep, that's a new word) in it. So each time I see $this, it is refering to any variable/functions within the class it's under?

 

I feel my eyes bugging as I write.  :'(

Link to comment
https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174951
Share on other sites

Alrighty, so here's bit of the first few lines. It's a calendar thingy. First line of the included Class is protected $ID; So, that means $this->ID = $ID."_"; in the below is refering to $ID from the included file controls.php (Plus the underscore)?

 

include_once("controls.php");

 

class Calendar extends Controls

{

.........

 

public function Calendar($ID, $Date = NULL)

{

$this->ID = $ID."_";

...

                ...

Link to comment
https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174961
Share on other sites

Yup, whats its doing is setting the 'protected $ID' - a property of the calendar to the value contained in $ID - the first argument of the function. That particular function is what's known as a constructor, and its called automatically when you create an instance of the object.

E.g

$cal = new Calendar(1,'2007-01-11'); //I dont know how it expects the date to be formatted so that may not work, its just an example
echo $cal->ID; //echos '1_' because the constructor has been called and the objects property has been set to 1 and concatenated with an underscore by the constructor

Because that property is marked as protected, it means that its value can only be changed by that class or any class that extends it. So if you tried to go

$cal->ID = "some crazy value";

You would get an error.

Link to comment
https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174979
Share on other sites

Thank you very much! My confusion was coming from the "extension" of the included file, had no idea that $this reached that document. This should definitely make it easier to pick it apart, but I'm sure I'll be back. I have to now figure out where the thing is hiding the resulting $_POST.

 

Onward!

And thanks again everyone.

 

Link to comment
https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-175063
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.