<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.spl-autoload-register" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>spl_autoload_register</refname>
  <refpurpose>Register given function as __autoload() implementation</refpurpose>
 </refnamediv>
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>bool</type><methodname>spl_autoload_register</methodname>
   <methodparam choice="opt"><type class="union"><type>callable</type><type>null</type></type><parameter>callback</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type>bool</type><parameter>throw</parameter><initializer>&true;</initializer></methodparam>
   <methodparam choice="opt"><type>bool</type><parameter>prepend</parameter><initializer>&false;</initializer></methodparam>
  </methodsynopsis>
  <para>
   Register a function with the spl provided __autoload queue. If the queue
   is not yet activated it will be activated.  
  </para>
  <para>
   If your code has an existing <function>__autoload</function> function then
   this function must be explicitly registered on the __autoload queue. This
   is because <function>spl_autoload_register</function> will effectively
   replace the engine cache for the <function>__autoload</function> function
   by either <function>spl_autoload</function> or
   <function>spl_autoload_call</function>.
  </para>
  <para>
   If there must be multiple autoload functions, <function>spl_autoload_register</function> 
   allows for this. It effectively creates a queue of autoload functions, and
   runs through each of them in the order they are defined. By contrast, 
   <function>__autoload</function> may only be defined once.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>callback</parameter></term>
     <listitem>
      <para>
       The autoload function being registered.
       If &null;, then the default implementation of
       <function>spl_autoload</function> will be registered.      
      </para>
      <methodsynopsis>
       <type>void</type><methodname><replaceable>callback</replaceable></methodname>
       <methodparam><type>string</type><parameter>class</parameter></methodparam>
      </methodsynopsis>
      <para>
       The <parameter>class</parameter> will not contain the leading
       backslash of a fully-qualified identifier.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>throw</parameter></term>
     <listitem>
      <para>
       This parameter specifies whether
       <function>spl_autoload_register</function> should throw 
       exceptions when the <parameter>callback</parameter>
       cannot be registered.
      </para>
      <warning>
        <para>
          This parameter is ignored as of PHP 8.0.0, and a notice will be
          emitted if set to &false;. <function>spl_autoload_register</function>
          will now always throw a <classname>TypeError</classname> on invalid
          arguments.
        </para>
      </warning>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>prepend</parameter></term>
     <listitem>
      <para>
       If true, <function>spl_autoload_register</function> will prepend
       the autoloader on the autoload queue instead of appending it.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

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

 <refsect1 role="changelog">
  &reftitle.changelog;
  <para>
   <informaltable>
    <tgroup cols="2">
     <thead>
      <row>
       <entry>&Version;</entry>
       <entry>&Description;</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>8.0.0</entry>
       <entry>
        <parameter>callback</parameter> is now nullable.
       </entry>
      </row>
     </tbody>
    </tgroup>
   </informaltable>
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title><function>spl_autoload_register</function> as a replacement for an <function>__autoload</function> function</title>
    <programlisting role="php">
<![CDATA[
<?php

// function __autoload($class) {
//     include 'classes/' . $class . '.class.php';
// }

function my_autoloader($class) {
    include 'classes/' . $class . '.class.php';
}

spl_autoload_register('my_autoloader');

// Or, using an anonymous function
spl_autoload_register(function ($class) {
    include 'classes/' . $class . '.class.php';
});

?>
]]>
    </programlisting>
   </example>
   <example>
    <title><function>spl_autoload_register</function> example where the class is not loaded</title>
    <programlisting role="php">
<![CDATA[
<?php

namespace Foobar;

class Foo {
    static public function test($class) {
        print '[['. $class .']]';
    }
}

spl_autoload_register(__NAMESPACE__ .'\Foo::test');

new InexistentClass;

?>
]]>
    </programlisting>
    &example.outputs.similar;
    <screen>
<![CDATA[
[[Foobar\InexistentClass]]
Fatal error: Class 'Foobar\InexistentClass' not found in ...
]]>
    </screen>
   </example>
   <example>
    <title>The identifier will be provided without the leading backslash</title>
    <programlisting role="php">
<![CDATA[
<?php

spl_autoload_register(static function ($class) {
    var_dump($class);
});

class_exists('RelativeName');
class_exists('RelativeName\\WithNamespace');
class_exists('\\AbsoluteName');
class_exists('\\AbsoluteName\\WithNamespace');

?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
string(12) "RelativeName"
string(26) "RelativeName\WithNamespace"
string(12) "AbsoluteName"
string(26) "AbsoluteName\WithNamespace"
]]>
    </screen>
   </example>
  </para>
 </refsect1>
 
 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>__autoload</function></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
-->
