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/Building-A-PHPBased-Mail-Client-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:

$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.

Recommended Answers

All 8 Replies

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

To clarify, does the line

$this = $parts[$x];

appear within something like the following

class NameOfTheObject
{
}

To clarify, does the line

$this = $parts[$x];

appear within something like the following

class NameOfTheObject
{
}

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.

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 tags.[code=php] tags.

Josh Connerty

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

<?
// define some constants
// message types
$type = array("text", "multipart", "message", "application", "audio",
"image", "video", "other");
// message encodings
$encoding = array("7bit", "8bit", "binary", "base64", "quoted-printable",
"other");

// parse message body
function parse($structure)
{
	global $type;
	global $encoding;

	// create an array to hold message sections
	$ret = array();

	// split structure into parts
	$parts = $structure->parts;

	for($x=0; $x<sizeof($parts); $x++)
	{
  $ret[$x]["pid"] = ($x+1);

  $this = $parts[$x];

  // default to text
  if ($this->type == "") { $this->type = 0; }
  $ret[$x]["type"] = $type[$this->type] . "/" . strtolower($this->subtype);

  // default to 7bit
  if ($this->encoding == "") { $this->encoding = 0; }
  $ret[$x]["encoding"] = $encoding[$this->encoding];

  $ret[$x]["size"] = strtolower($this->bytes);

  $ret[$x]["disposition"] = strtolower($this->disposition);

  if (strtolower($this->disposition) == "attachment")
  {
  	$params = $this->dparameters;
  	foreach ($params as $p)
  	{
    if($p->attribute == "FILENAME")
    {
    $ret[$x]["name"] = $p->value;
    break;
    }
  	}
  }
}

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

It looks like you have Object Oriented code there, if the file in question called by another file (include, require....)?

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.

commented: Love your sig :D +2

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.