[FuelPHP 1.7] Extend the Autoloader class
table of contents [非表示]
This is Kusakabe from the WEB team.
I was wondering how to extend the Autoloader class in FuelPHP 1.7, so I'm writing this down!
What I want to do
Extend the \Fuel\Core\Autoloader class and define it as an Autoloader class
How to extend FuelPHP's core classes
http://fuelphp.jp/docs/1.7/general/extending_core.html (FuelPHP 1.7)
As written here.
It's easier if you want to extend the Response class instead of the Autoloader.
1 | class Response extends FuelCoreResponse { } |
You can save it as app/classes/response.php and write the correspondence between class name and file path in add_classes of app/bootstrap.php.
1 | Autoloader::add_classes( array ( 'Response' => APPPATH. 'classes/response.php' , )); |
However, when I try to extend Autoloader using the same steps, it doesn't work.
Why? Because Autoloader is an exception!
Where I was worried
This is what is written at the bottom of the document
Autoloader
The Autoloader class is a special case, you can only extend it once as [
Autoloader]
and have it used. After extending it you have to require it manually in the [ app/bootstrap.php] file after the original [FuelCoreAutoloader]
, don 't forget to remove the line that aliases the core class to global.
In other words, this is what it means.
- Manually require the Autoloader class file that extends FuelCoreAutoloader in app/bootstrap.php
- remove the line that aliases the core class to global (???)
I understand the first one! Just do it like this.
Create app/classes/autoloader.php and
1 | class Autoloader extends FuelCoreAutoloader { } |
At the beginning of app/bootatrap.php
1 | require APPPATH. 'classes' .DIRECTORY_SEPARATOR. 'autoloader.php' ; |
Describe.
I didn't understand the second meaning! I didn't understand, so I ignored it and opened the page.
Cannot redeclare class Autoloader
I've been having trouble with this for a while, but I've solved it.
From the conclusion, around line 50 of public/index.php
1 | class_alias( 'Fuel\Core\Autoloader' , 'Autoloader' ); |
It's ok if you comment this out.
What this line means is
"Create an alias from Autoloader in the global namespace to Autoloader in the FuelCore namespace"
is. See http://php.net/manual/function.class-alias.php for class_alias()
Because of this alias,
1 | class Autoloader extends FuelCoreAutoloader { } |
In this case, Autoloader was defined twice and inherited itself.
In summary,
When extending the core class Autoloader
- Create app/classes/autoloader.php
1
class
Autoloader
extends
FuelCoreAutoloader { }
- At the beginning of app/bootstrap.php
1
require
APPPATH.
'classes'
.DIRECTORY_SEPARATOR.
'autoloader.php'
;
describe
- public/index.php (around line 50)
1
class_alias(
'Fuel\Core\Autoloader'
,
'Autoloader'
);
comment out
It is OK as long as you keep these three points in check. Looks like things are going well later.