Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
b6c00809
Commit
b6c00809
authored
Jul 13, 2014
by
Larry Ullman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Edited; already very clean!
parent
76e5875a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
16 deletions
+16
-16
concept-autoloading.md
docs/guide/concept-autoloading.md
+16
-16
No files found.
docs/guide/concept-autoloading.md
View file @
b6c00809
...
...
@@ -2,7 +2,7 @@ Class Autoloading
=================
Yii relies on the
[
class autoloading mechanism
](
http://www.php.net/manual/en/language.oop5.autoload.php
)
to locate and include required class files. It provides a high-performance class autoloader that is compliant to the
to locate and include
all
required class files. It provides a high-performance class autoloader that is compliant to the
[
PSR-4 standard
](
https://github.com/php-fig/fig-standards/blob/master/proposed/psr-4-autoloader/psr-4-autoloader.md
)
.
The autoloader is installed when you include the
`Yii.php`
file.
...
...
@@ -15,7 +15,7 @@ Using the Yii Autoloader <a name="using-yii-autoloader"></a>
To make use of the Yii class autoloader, you should follow two simple rules when creating and naming your classes:
*
Each class must be under
some namespace (e.g.
`foo\bar\MyClass`
).
*
Each class must be under
a namespace (e.g.
`foo\bar\MyClass`
)
*
Each class must be saved in an individual file whose path is determined by the following algorithm:
```
php
...
...
@@ -23,30 +23,30 @@ To make use of the Yii class autoloader, you should follow two simple rules when
$classFile
=
Yii
::
getAlias
(
'@'
.
str_replace
(
'\\'
,
'/'
,
$className
)
.
'.php'
);
```
For example, if a class name is
`foo\bar\MyClass`
, the
[
alias
](
concept-aliases.md
)
for the corresponding class file path
would be
`@foo/bar/MyClass.php`
. In order for this alias to be
able to be resolved
into a file path,
For example, if a class name
and namespace
is
`foo\bar\MyClass`
, the
[
alias
](
concept-aliases.md
)
for the corresponding class file path
would be
`@foo/bar/MyClass.php`
. In order for this alias to be
resolvable
into a file path,
either
`@foo`
or
`@foo/bar`
must be a
[
root alias
](
concept-aliases.md#defining-aliases
)
.
When
you are
using the
[
Basic Application Template
](
start-basic.md
)
, you may put your classes under the top-level
When using the
[
Basic Application Template
](
start-basic.md
)
, you may put your classes under the top-level
namespace
`app`
so that they can be autoloaded by Yii without the need of defining a new alias. This is because
`@app`
is a
[
predefined alias
](
concept-aliases.md#predefined-aliases
)
, and a class name like
`app\components\MyClass`
can be resolved into the class file
`AppBasePath/components/MyClass.php`
, according to the algorithm
we
just described.
can be resolved into the class file
`AppBasePath/components/MyClass.php`
, according to the algorithm just described.
In the
[
Advanced Application Template
](
tutorial-advanced-app.md
)
, each tier has its own root alias. For example,
the front-end tier has a root alias
`@frontend`
while the back-end tier
`@backend`
. As a result, you may
put the front-end classes under the namespace
`frontend`
while the back-end classes under
`backend`
. This will
the front-end tier has a root alias
`@frontend`
,
while the back-end tier
`@backend`
. As a result, you may
put the front-end classes under the namespace
`frontend`
while the back-end classes
are
under
`backend`
. This will
allow these classes to be autoloaded by the Yii autoloader.
Class Map <a name="class-map"></a>
---------
The Yii class autoloader supports the
*class map*
feature which maps class names to the corresponding class file paths.
The Yii class autoloader supports the
*class map*
feature
,
which maps class names to the corresponding class file paths.
When the autoloader is loading a class, it will first check if the class is found in the map. If so, the corresponding
file path will be included directly without further check. This makes class autoloading super fast. In fact,
all core Yii classes are
being
autoloaded this way.
all core Yii classes are autoloaded this way.
You may add a class to the class map
`Yii::$classMap`
as follows,
You may add a class to the class map
, stored in
`Yii::$classMap`
, using:
```
php
Yii
::
$classMap
[
'foo\bar\MyClass'
]
=
'path/to/MyClass.php'
;
...
...
@@ -60,14 +60,14 @@ Using Other Autoloaders <a name="using-other-autoloaders"></a>
-----------------------
Because Yii embraces Composer as a package dependency manager, it is recommended that you also install
the Composer autoloader. If you are using
some 3rd-party libraries that have their
autoloaders, you should
also install th
em
.
the Composer autoloader. If you are using
3rd-party libraries that have their own
autoloaders, you should
also install th
ose
.
When
you are
using the Yii autoloader together with other autoloaders, you should include the
`Yii.php`
file
*after*
all other autoloaders are installed. This will make the Yii autoloader t
o be t
he first one responding to
When using the Yii autoloader together with other autoloaders, you should include the
`Yii.php`
file
*after*
all other autoloaders are installed. This will make the Yii autoloader the first one responding to
any class autoloading request. For example, the following code is extracted from
the
[
entry script
](
structure-entry-scripts.md
)
of the
[
Basic Application Template
](
start-basic.md
)
. The first
line installs the Composer autoloader, while the second line installs the Yii autoloader
.
line installs the Composer autoloader, while the second line installs the Yii autoloader
:
```
php
require
(
__DIR__
.
'/../vendor/autoload.php'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment