php-doc-en/reference/yaf/yaf-loader.xml

301 lines
8.6 KiB
XML
Raw Permalink Normal View History

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<phpdoc:classref xml:id="class.yaf-loader" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>The Yaf_Loader class</title>
<titleabbrev>Yaf_Loader</titleabbrev>
<partintro>
<!-- {{{ Yaf_Loader intro -->
<section xml:id="yaf-loader.intro">
&reftitle.intro;
<para>
<classname>Yaf_Loader</classname> introduces a comprehensive autoloading
solution for Yaf.
</para>
<para>
The first time an instance of <classname>Yaf_Application</classname> is retrieved,
<classname>Yaf_Loader</classname> will instance a singleton, and registers itself with
spl_autoload. You retrieve an instance using the <methodname>Yaf_Loader::getInstance</methodname>
</para>
<para>
<classname>Yaf_Loader</classname> attempt to load a class only one shot, if
failed, depend on <link
linkend="ini.yaf.use-spl-autoload">yaf.use_spl_auload</link>, if this
config is On <methodname>Yaf_Loader::autoload</methodname> will return
&false;, thus give the chance to other autoload function. if it is Off
(by default), <methodname>Yaf_Loader::autoload</methodname> will return
&true;, and more important is that a very useful warning will be triggered
(very useful to find out why a class could not be loaded).
<note>
<para>
Please keep yaf.use_spl_autoload Off unless there is some library have
their own autoload mechanism and impossible to rewrite it.
</para>
</note>
</para>
<para>
By default, <classname>Yaf_Loader</classname> assume all library (class
defined script) store in the <link linkend="ini.yaf.library">global library
directory</link>, which is defined in the php.ini(yaf.library).
</para>
<para>
If you want <classname>Yaf_Loader</classname> search some
classes(libraries) in the <link linkend="yaf-loader.props.library">local class
directory</link>(which is defined in application.ini, and by default,
it is <link
linkend="configuration.yaf.directory">application.directory</link> . "/library"),
you should register the class prefix using the
<methodname>Yaf_Loader::registerLocalNameSpace</methodname>
</para>
<para>
Let's see some examples(assuming APPLICATION_PATH is <link
linkend="configuration.yaf.directory">application.directory</link>):
<example>
<title>Config example</title>
<programlisting role="shell">
<![CDATA[
// Assuming the following configure in php.ini:
yaf.library = "/global_dir"
//Assuming the following configure in application.ini
application.library = APPLICATION_PATH "/library"
]]>
</programlisting>
</example>
Assuming the following local name space is registered:
<example>
<title>Register localnamespace</title>
<programlisting role="php">
<![CDATA[
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
public function _initLoader($dispatcher) {
Yaf_Loader::getInstance()->registerLocalNameSpace(array("Foo", "Bar"));
}
?>
]]>
</programlisting>
</example>
Then the autoload examples:
<example>
<title>Load class example</title>
<programlisting role="shell">
<![CDATA[
class Foo_Bar_Test =>
// APPLICATION_PATH/library/Foo/Bar/Test.php
class GLO_Name =>
// /global_dir/Glo/Name.php
class BarNon_Test
// /global_dir/Barnon/Test.php
]]>
</programlisting>
</example>
<example>
<title>Load namespace class example</title>
<programlisting role="shell">
<![CDATA[
class \Foo\Bar\Dummy =>
// APPLICATION_PATH/library/Foo/Bar/Dummy.php
class \FooBar\Bar\Dummy =>
// /global_dir/FooBar/Bar/Dummy.php
]]>
</programlisting>
</example>
</para>
<para>
You may noticed that all the folder with the first letter capitalized, you can make them
lowercase by set <link linkend="ini.yaf.lowcase-path">yaf.lowcase_path</link> = On in php.ini
</para>
<para>
<classname>Yaf_Loader</classname> is also designed to load the MVC classes,
and the rule is:
<example>
<title>MVC class loading example</title>
<programlisting role="shell">
<![CDATA[
Controller Classes =>
// APPLICATION_PATH/controllers/
Model Classes =>
// APPLICATION_PATH/models/
Plugin Classes =>
// APPLICATION_PATH/plugins/
]]>
</programlisting>
</example>
Yaf identify a class's suffix(this is by default, you can also change to
the prefix by change the configure <link
linkend="ini.yaf.name-suffix">yaf.name_suffix</link>) to decide whether it
is a MVC class:
<example>
<title>MVC class distinctions</title>
<programlisting role="shell">
<![CDATA[
Controller Classes =>
// ***Controller
Model Classes =>
// ***Model
Plugin Classes =>
// ***Plugin
]]>
</programlisting>
</example>
some examples:
<example>
<title>MVC loading example</title>
<programlisting role="shell">
<![CDATA[
class IndexController
// APPLICATION_PATH/controllers/Index.php
class DataModel =>
// APPLICATION_PATH/models/Data.php
class DummyPlugin =>
// APPLICATION_PATH/plugins/Dummy.php
class A_B_TestModel =>
// APPLICATION_PATH/models/A/B/Test.php
]]>
</programlisting>
</example>
<note>
<para>
As of 2.1.18, Yaf supports Controllers autoloading for user script side,
(which means the autoloading triggered by user php script, eg: access a
controller static property in Bootstrap or Plugins),
but autoloader only try to locate controller class script under the default
module folder, which is "APPLICATION_PATH/controllers/".
</para>
</note>
also, the directory will be affected by <link linkend="ini.yaf.lowcase-path">yaf.lowcase_path</link>.
</para>
</section>
<!-- }}} -->
<section xml:id="yaf-loader.synopsis">
&reftitle.classsynopsis;
<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>Yaf_Loader</classname></ooclass>
<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<ooclass>
<classname>Yaf_Loader</classname>
</ooclass>
</classsynopsisinfo>
<!-- }}} -->
<classsynopsisinfo role="comment">&Properties;</classsynopsisinfo>
<fieldsynopsis>
<modifier>protected</modifier>
<varname linkend="yaf-loader.props.local-ns">_local_ns</varname>
</fieldsynopsis>
<fieldsynopsis>
<modifier>protected</modifier>
<varname linkend="yaf-loader.props.library">_library</varname>
</fieldsynopsis>
<fieldsynopsis>
<modifier>protected</modifier>
<varname linkend="yaf-loader.props.global-library">_global_library</varname>
</fieldsynopsis>
<fieldsynopsis>
<modifier>static</modifier>
<varname linkend="yaf-loader.props.instance">_instance</varname>
</fieldsynopsis>
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.yaf-loader')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[1])" />
</classsynopsis>
<!-- }}} -->
</section>
<!-- {{{ Yaf_Loader properties -->
<section xml:id="yaf-loader.props">
&reftitle.properties;
<variablelist>
<varlistentry xml:id="yaf-loader.props.local-ns">
<term><varname>_local_ns</varname></term>
<listitem>
<para></para>
</listitem>
</varlistentry>
<varlistentry xml:id="yaf-loader.props.library">
<term><varname>_library</varname></term>
<listitem>
<para>
By default, this value is <link
linkend="configuration.yaf.directory">application.directory</link> . "/library",
you can change this either in the application.ini(application.library) or call to
<methodname>Yaf_Loader::setLibraryPath</methodname>
</para>
</listitem>
</varlistentry>
<varlistentry xml:id="yaf-loader.props.global-library">
<term><varname>_global_library</varname></term>
<listitem>
<para></para>
</listitem>
</varlistentry>
<varlistentry xml:id="yaf-loader.props.instance">
<term><varname>_instance</varname></term>
<listitem>
<para></para>
</listitem>
</varlistentry>
</variablelist>
</section>
<!-- }}} -->
</partintro>
&reference.yaf.entities.yaf-loader;
</phpdoc:classref>
<!-- 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
-->