2011-09-01 02:22:00 +00:00
|
|
|
<?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>
|
2011-11-26 05:13:58 +00:00
|
|
|
<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>
|
2011-11-29 07:15:43 +00:00
|
|
|
<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
|
2012-11-02 04:08:40 +00:00
|
|
|
&false;, thus give the chance to other autoload function. if it is Off
|
2011-11-29 07:15:43 +00:00
|
|
|
(by default), <methodname>Yaf_Loader::autoload</methodname> will return
|
2019-02-24 20:38:06 +00:00
|
|
|
&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).
|
2011-11-29 07:15:43 +00:00
|
|
|
<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
|
2011-11-26 05:13:58 +00:00
|
|
|
directory</link>, which is defined in the php.ini(yaf.library).
|
2011-09-01 02:22:00 +00:00
|
|
|
</para>
|
2011-11-26 05:13:58 +00:00
|
|
|
|
|
|
|
<para>
|
2011-11-29 07:15:43 +00:00
|
|
|
If you want <classname>Yaf_Loader</classname> search some
|
|
|
|
classes(libraries) in the <link linkend="yaf-loader.props.library">local class
|
2011-11-26 05:13:58 +00:00
|
|
|
directory</link>(which is defined in application.ini, and by default,
|
2011-12-02 03:53:40 +00:00
|
|
|
it is <link
|
2013-08-16 09:25:19 +00:00
|
|
|
linkend="configuration.yaf.directory">application.directory</link> . "/library"),
|
2011-12-02 03:53:40 +00:00
|
|
|
you should register the class prefix using the
|
2011-11-26 05:13:58 +00:00
|
|
|
<methodname>Yaf_Loader::registerLocalNameSpace</methodname>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2011-12-02 03:53:40 +00:00
|
|
|
Let's see some examples(assuming APPLICATION_PATH is <link
|
|
|
|
linkend="configuration.yaf.directory">application.directory</link>):
|
2011-11-26 15:53:03 +00:00
|
|
|
<example>
|
|
|
|
<title>Config example</title>
|
|
|
|
<programlisting role="shell">
|
2011-11-26 05:13:58 +00:00
|
|
|
<![CDATA[
|
|
|
|
// Assuming the following configure in php.ini:
|
2013-08-16 09:25:19 +00:00
|
|
|
yaf.library = "/global_dir"
|
2011-11-26 05:13:58 +00:00
|
|
|
|
|
|
|
//Assuming the following configure in application.ini
|
2013-08-16 09:25:19 +00:00
|
|
|
application.library = APPLICATION_PATH "/library"
|
2011-11-26 15:53:03 +00:00
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
2011-11-26 05:13:58 +00:00
|
|
|
|
2019-02-24 20:38:06 +00:00
|
|
|
Assuming the following local name space is registered:
|
2011-11-26 15:53:03 +00:00
|
|
|
<example>
|
|
|
|
<title>Register localnamespace</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
2011-11-26 05:13:58 +00:00
|
|
|
<?php
|
|
|
|
class Bootstrap extends Yaf_Bootstrap_Abstract{
|
2011-11-26 15:53:03 +00:00
|
|
|
public function _initLoader($dispatcher) {
|
|
|
|
Yaf_Loader::getInstance()->registerLocalNameSpace(array("Foo", "Bar"));
|
|
|
|
}
|
2011-11-26 05:13:58 +00:00
|
|
|
?>
|
|
|
|
]]>
|
2011-11-26 15:53:03 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
2011-11-26 05:13:58 +00:00
|
|
|
|
2011-11-26 15:53:03 +00:00
|
|
|
Then the autoload examples:
|
|
|
|
<example>
|
|
|
|
<title>Load class example</title>
|
|
|
|
<programlisting role="shell">
|
2011-11-26 05:13:58 +00:00
|
|
|
<![CDATA[
|
|
|
|
class Foo_Bar_Test =>
|
|
|
|
// APPLICATION_PATH/library/Foo/Bar/Test.php
|
|
|
|
|
|
|
|
class GLO_Name =>
|
|
|
|
// /global_dir/Glo/Name.php
|
|
|
|
|
2011-11-26 05:19:57 +00:00
|
|
|
class BarNon_Test
|
|
|
|
// /global_dir/Barnon/Test.php
|
|
|
|
]]>
|
2011-11-26 15:53:03 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
2011-11-26 05:19:57 +00:00
|
|
|
|
2011-11-26 15:53:03 +00:00
|
|
|
As of PHP 5.3, you can use namespace:
|
|
|
|
<example>
|
|
|
|
<title>Load namespace class example</title>
|
|
|
|
<programlisting role="shell">
|
2011-11-26 05:19:57 +00:00
|
|
|
<![CDATA[
|
2011-11-26 05:13:58 +00:00
|
|
|
class \Foo\Bar\Dummy =>
|
|
|
|
// APPLICATION_PATH/library/Foo/Bar/Dummy.php
|
|
|
|
|
|
|
|
class \FooBar\Bar\Dummy =>
|
|
|
|
// /global_dir/FooBar/Bar/Dummy.php
|
|
|
|
]]>
|
2011-11-26 15:53:03 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
2011-11-26 05:13:58 +00:00
|
|
|
</para>
|
2011-11-26 15:53:03 +00:00
|
|
|
|
2011-11-26 05:13:58 +00:00
|
|
|
<para>
|
2019-02-24 20:38:06 +00:00
|
|
|
You may noticed that all the folder with the first letter capitalized, you can make them
|
2011-11-26 05:49:49 +00:00
|
|
|
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:
|
2011-11-26 15:53:03 +00:00
|
|
|
<example>
|
|
|
|
<title>MVC class loading example</title>
|
|
|
|
<programlisting role="shell">
|
2011-11-26 05:49:49 +00:00
|
|
|
<![CDATA[
|
|
|
|
Controller Classes =>
|
2011-11-26 15:53:03 +00:00
|
|
|
// APPLICATION_PATH/controllers/
|
2011-11-26 05:49:49 +00:00
|
|
|
|
|
|
|
Model Classes =>
|
2011-11-26 15:53:03 +00:00
|
|
|
// APPLICATION_PATH/models/
|
2011-11-26 05:49:49 +00:00
|
|
|
|
|
|
|
Plugin Classes =>
|
2011-11-26 15:53:03 +00:00
|
|
|
// APPLICATION_PATH/plugins/
|
2011-11-26 05:49:49 +00:00
|
|
|
]]>
|
2011-11-26 15:53:03 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
2011-11-26 05:49:49 +00:00
|
|
|
|
|
|
|
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:
|
2011-11-26 15:53:03 +00:00
|
|
|
<example>
|
|
|
|
<title>MVC class distinctions</title>
|
2011-11-26 09:08:39 +00:00
|
|
|
<programlisting role="shell">
|
2011-11-26 05:49:49 +00:00
|
|
|
<![CDATA[
|
|
|
|
Controller Classes =>
|
|
|
|
// ***Controller
|
|
|
|
|
|
|
|
Model Classes =>
|
|
|
|
// ***Model
|
|
|
|
|
|
|
|
Plugin Classes =>
|
|
|
|
// ***Plugin
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
2011-11-26 15:53:03 +00:00
|
|
|
</example>
|
2011-11-26 05:49:49 +00:00
|
|
|
|
|
|
|
some examples:
|
2011-11-26 15:53:03 +00:00
|
|
|
<example>
|
|
|
|
<title>MVC loading example</title>
|
|
|
|
<programlisting role="shell">
|
2011-11-26 05:49:49 +00:00
|
|
|
<![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
|
|
|
|
]]>
|
2011-11-26 15:53:03 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
2011-11-26 05:49:49 +00:00
|
|
|
|
2012-10-10 02:57:31 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2012-10-10 03:08:34 +00:00
|
|
|
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/".
|
2012-10-10 02:57:31 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
2011-11-26 05:49:49 +00:00
|
|
|
also, the directory will be affected by <link linkend="ini.yaf.lowcase-path">yaf.lowcase_path</link>.
|
2011-11-26 05:13:58 +00:00
|
|
|
</para>
|
2011-09-01 02:22:00 +00:00
|
|
|
</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>
|
2011-11-26 05:13:58 +00:00
|
|
|
<para>
|
2011-12-02 03:53:40 +00:00
|
|
|
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
|
2011-11-26 05:13:58 +00:00
|
|
|
<methodname>Yaf_Loader::setLibraryPath</methodname>
|
|
|
|
</para>
|
2011-09-01 02:22:00 +00:00
|
|
|
</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>
|
2011-11-26 05:13:58 +00:00
|
|
|
|
2011-09-01 02:22:00 +00:00
|
|
|
|
|
|
|
&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
|
|
|
|
-->
|