PHPWomen Site Home » Programming » PHP Coding » $this syntax
$this syntax [message #2860] Tue, 20 October 2009 11:21 Go to next message
jmangin  is currently offline jmangin
Messages: 4
Registered: September 2008
Location: Maryland
Shiny and New
I've inherited some code that I need to adapt for another purpose. There's a syntax using $this with which I am not familiar. I've seen it before, but I never been taught that style of coding.

e.g.:

function getError($name) {
if($this->isError($name)) {
return $this->errors[$name];
} else {
return false;
}
}


I tried to google "$this", but I can't find it because the search engine ignores the dollar sign and I get every post with the word "this" in it!

So, what is the function of $this? What is the type of syntax called, so I can find the documentation and read more about it.

Thanks,

Julie
Re: $this syntax [message #2861 is a reply to message #2860 ] Wed, 21 October 2009 06:42 Go to previous messageGo to next message
lorna  is currently offline lorna
Messages: 413
Registered: October 2006
Location: Leeds, UK
Feeling Comfortable

Julie,

This is Object Oriented Programming - you should be able to find some introductory tutorials showing how "$this" is used. Basically $this is an object and the arrow notation -> shows that something is a property of that object. A property is just a variable but it belongs to an object.

So instead of setting $error = 'gone wrong' and returning $error, you set $this->error = 'message of failure' and then you can get that value back again from that object.

Hope that makes some sense - find a tutorial, have a read, and post again if you have any questions, we're always happy to help!

Lorna


Lorna Mitchell (online at http://www.lornajane.net)
Re: $this syntax [message #2862 is a reply to message #2861 ] Wed, 21 October 2009 08:13 Go to previous messageGo to next message
jmangin  is currently offline jmangin
Messages: 4
Registered: September 2008
Location: Maryland
Shiny and New
Thank you, Lorna! I work at a library, and I can get my hands on some books about object-oriented PHP.
Re: $this syntax [message #3029 is a reply to message #2860 ] Sun, 07 March 2010 19:38 Go to previous messageGo to next message
Amanda  is currently offline Amanda
Messages: 33
Registered: January 2007
Location: Pennsylvania
Shiny and New

I still have trouble with $this myself. If you find a helpful tutorial online in your search do you mind posting the link?

I work a lot with code that uses it but when I try and write a new code block that implements it I know I use it sometimes when I don't need to, or incorrectly, or all over the place randomly. Kind of like a comma in Flowers for Algernon. LOL
Re: $this syntax [message #3032 is a reply to message #3029 ] Mon, 08 March 2010 04:54 Go to previous message
nurshah  is currently offline nurshah
Messages: 1
Registered: March 2010
Shiny and New
Hi all i hope this site helps..

http://devzone.zend.com/node/view/id/638

Previous Topic:Remembering Logins
Next Topic:insert image inside textbox
Goto Forum:
  


Current Time: Fri Jul 30 11:46:25 EDT 2010

Total time taken to generate the page: 0.01069 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.0.
Copyright ©2001-2006 FUD Forum Bulletin Board Software