943,865 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 567
  • PHP RSS
Aug 22nd, 2009
0

Problem with $this

Expand Post »
Hi

I've been doing a study of some PHP code developed by another author in 2002.

The article could be found at:
http://www.devshed.com/c/a/PHP/Build...ient-part-2/3/

The functions I need help with in understanding are:
function parse($structure) //custom function developed by author
function get_attachments($arr) //custom function developed by author

The following line gave me difficulties:
PHP Syntax (Toggle Plain Text)
  1. $this = $parts[$x];

I have studied this line of code and would like to know if $this has some sort of special meaning in PHP?

I tried to change $this to $result and the application seemed to be working, but not giving the correct result.

Any help would be appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hugo Brand is offline Offline
13 posts
since Nov 2007
Aug 22nd, 2009
0

Re: Problem with $this

Click to Expand / Collapse  Quote originally posted by Hugo Brand ...
I have studied this line of code and would like to know if $this has some sort of special meaning in PHP?
Yes, $this is used within classes. Is this code object oriented, i.e. has the author included any classes? If not, $this should not be used.

If it is object oriented code, $this may be used deliberately and changing it to something else might make one bit of your code work fine and break it elsewhere.

I would suggest taking a look at this: http://us3.php.net/manual/en/language.oop5.basic.php
Reputation Points: 14
Solved Threads: 10
Junior Poster in Training
SimonMayer is offline Offline
53 posts
since Apr 2008
Aug 22nd, 2009
0

Re: Problem with $this

To clarify, does the line
PHP Syntax (Toggle Plain Text)
  1. $this = $parts[$x];
appear within something like the following
PHP Syntax (Toggle Plain Text)
  1. class NameOfTheObject
  2. {
  3. }
Reputation Points: 14
Solved Threads: 10
Junior Poster in Training
SimonMayer is offline Offline
53 posts
since Apr 2008
Aug 22nd, 2009
0

Re: Problem with $this

Click to Expand / Collapse  Quote originally posted by SimonMayer ...
To clarify, does the line
PHP Syntax (Toggle Plain Text)
  1. $this = $parts[$x];
appear within something like the following
PHP Syntax (Toggle Plain Text)
  1. class NameOfTheObject
  2. {
  3. }
SimonMayer

No, from reading the line code it looks as if it appeared only in a function, not a class.

Thank you for providing a link, I will look at it when I have some time available.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hugo Brand is offline Offline
13 posts
since Nov 2007
Aug 22nd, 2009
0

Re: Problem with $this

Then no $this should not change the script, the issue you are probably having is that the varibale is being called later on in the script. You may need to "Find" $this using your text editor and change it to $result.

Please post your current document in [code=php] tags.
Reputation Points: 31
Solved Threads: 27
Unverified User
Josh Connerty is offline Offline
342 posts
since Apr 2009
Aug 23rd, 2009
0

Re: Problem with $this

Josh Connerty

This reply contains the original code in respect to the author who developed it, that I was studying.

PHP Syntax (Toggle Plain Text)
  1. <?
  2. // define some constants
  3. // message types
  4. $type = array("text", "multipart", "message", "application", "audio",
  5. "image", "video", "other");
  6. // message encodings
  7. $encoding = array("7bit", "8bit", "binary", "base64", "quoted-printable",
  8. "other");
  9.  
  10. // parse message body
  11. function parse($structure)
  12. {
  13. global $type;
  14. global $encoding;
  15.  
  16. // create an array to hold message sections
  17. $ret = array();
  18.  
  19. // split structure into parts
  20. $parts = $structure->parts;
  21.  
  22. for($x=0; $x<sizeof($parts); $x++)
  23. {
  24. $ret[$x]["pid"] = ($x+1);
  25.  
  26. $this = $parts[$x];
  27.  
  28. // default to text
  29. if ($this->type == "") { $this->type = 0; }
  30. $ret[$x]["type"] = $type[$this->type] . "/" . strtolower($this->subtype);
  31.  
  32. // default to 7bit
  33. if ($this->encoding == "") { $this->encoding = 0; }
  34. $ret[$x]["encoding"] = $encoding[$this->encoding];
  35.  
  36. $ret[$x]["size"] = strtolower($this->bytes);
  37.  
  38. $ret[$x]["disposition"] = strtolower($this->disposition);
  39.  
  40. if (strtolower($this->disposition) == "attachment")
  41. {
  42. $params = $this->dparameters;
  43. foreach ($params as $p)
  44. {
  45. if($p->attribute == "FILENAME")
  46. {
  47. $ret[$x]["name"] = $p->value;
  48. break;
  49. }
  50. }
  51. }
  52. }
  53.  
  54. return $ret;
}
?>

Changing all $this to $result, made the application work, but with unexpected results. I have debugged most of the conditional statements above and my major problem lied with this if statement:

if (strtolower($this->disposition) == "attachment")

It was suppose to read the conditional statement, but instead it doesn't find any value inside of the $this->disposition containing an "attachment" string value.

Any comments would still be appreciated.
Thanks
Last edited by Hugo Brand; Aug 23rd, 2009 at 2:30 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hugo Brand is offline Offline
13 posts
since Nov 2007
Aug 23rd, 2009
0

Re: Problem with $this

It looks like you have Object Oriented code there, if the file in question called by another file (include, require....)?
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Aug 23rd, 2009
1

Re: Problem with $this

Yes Xan is right, there is deffinetly object oriented coding in their. $this does seem to be a class of some sort.

Does $this really need to be changed? Else you are looking to rename the class. But first you need to find it.
Reputation Points: 31
Solved Threads: 27
Unverified User
Josh Connerty is offline Offline
342 posts
since Apr 2009
Aug 24th, 2009
0

Re: Problem with $this

Hi everyone. Thanks for the posts. I couldn't solve the problem.

Yes. I totally agree with the fact that this in most programming languages refers to object oriented code, but it doesn't look as if the author intended to use object oriented code within his example. If so, then I'm totally confused, because none of his previous code samples, even those that followed, demonstrated any form of object-orientation programming inside of PHP.

Thanks again, maybe I'll figure it out someday.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hugo Brand is offline Offline
13 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Parse error: syntax error, unexpected $end in /home/content/e/b/a/....
Next Thread in PHP Forum Timeline: user preferences





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC