<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="gearmanworker.addfunction" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>GearmanWorker::addFunction</refname>
  <refpurpose>Register and add callback function</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis role="GearmanWorker">
   <modifier>public</modifier> <type>bool</type><methodname>GearmanWorker::addFunction</methodname>
   <methodparam><type>string</type><parameter>function_name</parameter></methodparam>
   <methodparam><type>callable</type><parameter>function</parameter></methodparam>
   <methodparam choice="opt"><type>mixed</type><parameter>context</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type>int</type><parameter>timeout</parameter><initializer>0</initializer></methodparam>
  </methodsynopsis>
  <para>
   Registers a function name with the job server and specifies a callback corresponding to that
   function.  Optionally specify extra application context data to be used when the callback is
   called and a timeout.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>function_name</parameter></term>
     <listitem>
      <para>
       The name of a function to register with the job server
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>function</parameter></term>
     <listitem>
      <para>
       A callback that gets called when a job for the registered function name is submitted
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>context</parameter></term>
     <listitem>
      <para>
       A reference to arbitrary application context data that can be modified by the worker function
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>timeout</parameter></term>
     <listitem>
      <para>
       An interval of time in seconds
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   &return.success;
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title>Simple worker making use of extra application context data</title>
    <programlisting role="php">
<![CDATA[
<?php

# get a gearman worker
$worker= new GearmanWorker(); 

# add the default server (localhost)
$worker->addServer(); 

# define a variable to hold application data
$count= 0; 

# add the "reverse" function
$worker->addFunction("reverse", "reverse_cb", $count);

# start the worker
while ($worker->work());

function reverse_cb($job, &$count) 
{ 
  $count++; 
  return "$count: " . strrev($job->workload()); 
} 

?>
]]>
    </programlisting>
    <para>
     Running a client that submits two jobs for the reverse function would have
     output similar to the following:
    </para>
    <screen>
<![CDATA[
1: olleh
2: dlrow
]]>
    </screen>
   </example>
  </para>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><methodname>GearmanClient::do</methodname></member>
   </simplelist>
  </para>
 </refsect1>

</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
