<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code>&lt;?php
function get_handle($type)
{
	if (type=="r")
	{
		if (isset($this-&gt;fhr)) return $this-&gt;fhr;
		else user_error("File not open for reading");
	}
	elseif (type=="w")
	{
		if (isset($this-&gt;fhw)) return $this-&gt;fhw;
		else user_error("File not open for writing");
	}
	elseif (type=="a")
	{
		if (isset($this-&gt;fha)) return $this-&gt;fha;
		else user_error("File not open for appending");
	}
	elseif (!isset($type))
	{
		if (isset($this-&gt;fhr)) return $this-&gt;fhr;
		elseif (isset($this-&gt;fhw)) return $this-&gt;fhw;
		elseif (isset($this-&gt;fha)) return $this-&gt;fha;
		else user_error ("File not open");
	}
	else user_error("Invalid type");
}
?&gt;</code>
  <comment>This method feels like it's way more complicated than it should be.

Update: added &lt;?php to colourise.</comment>
  <created-at type="datetime">2010-02-09T00:35:08+00:00</created-at>
  <id type="integer">1171</id>
  <language>PHP</language>
  <permalink>return-the-appropriate-file-handle</permalink>
  <refactors-count type="integer">2</refactors-count>
  <title>Return the appropriate file handle</title>
  <trackback-url></trackback-url>
  <updated-at type="datetime">2010-02-09T10:17:05+00:00</updated-at>
  <user-id type="integer">1073</user-id>
  <refactors type="array">
    <refactor>
      <code>&lt;?php
function get_handle($type)
{
  // If everything is ok, just return what was requested.
  $mode = 'fh'.$type;
  if ( isset($this-&gt;$mode) )
  {
    return $this-&gt;$mode;
  }
	
  // If the handle is not set, generate an error.
  switch($type)
  {
    case 'r':
      user_error("File not open for reading");
      break;
    case 'w':
      user_error("File not open for writing");
      break;
    case 'a':
      user_error("File not open for appending");
      break;
    default:
      user_error("Invalid type");
  }
}
?&gt;</code>
      <code-id type="integer">1171</code-id>
      <comment>Your code isn't bad, but here is a cleaner version. $type will allways have to be set if you do not define a standard value in the function definition, so we can skip that part entirely.</comment>
      <created-at type="datetime">2010-02-09T08:03:26+00:00</created-at>
      <id type="integer">438716</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Return the appropriate file handle</title>
      <user-id type="integer">1904</user-id>
      <user-name>Christoffer</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>&lt;?php
function get_handle($type='!') // $type set to something invalid, so that user explicitly has to declare it.
{
  // If everything is ok, just return what was requested.
  $mode = 'fh'.$type;

  if ( isset($this-&gt;$mode) )
    return $this-&gt;$mode;

  $types = array('r' =&gt; 'reading', 'w' =&gt; 'writing', 'a' =&gt; 'appending');

  // Work out what kind of error to return
  if (in_array($type,array_keys($types)))
    user_error("File not open for ".$types[$type]);
  else
    user_error("Invalid type");
}
?&gt;</code>
      <code-id type="integer">1171</code-id>
      <comment>Thanks for that. It does make the code a lot simpler setting a default $type, but the ideal is that if the user leaves it blank, the function detects how the file has been opened (obviously it can be opened in more that one way, but this would be rare, and they shouldn't be leaving it blank if this is the case). Having said all that, I've had a go at refactoring your solution further!!!</comment>
      <created-at type="datetime">2010-02-09T10:17:03+00:00</created-at>
      <id type="integer">438764</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Return the appropriate file handle</title>
      <user-id type="integer">1073</user-id>
      <user-name>Nathan</user-name>
      <user-website nil="true"></user-website>
    </refactor>
  </refactors>
</code>
