[FuelPHP 1.7] Extend the Autoloader class

This is Kusakabe from the web team

I was struggling with how to extend the Autoloader class in FuelPHP 1.7, so I'll write it down!

About the expansion procedure

What I want to do

Extend the \Fuel\Core\Autoloader class and define it as an Autoloader class

 

How to extend FuelPHP core classes

http://fuelphp.jp/docs/1.7/general/extending_core.html (FuelPHP 1.7)

It's exactly as it says here

If you don't extend Autoloader but extend, say, the Response class, it's easy:

class Response extends FuelCoreResponse { }

Save it as app/classes/response.php and write the correspondence between the class name and file path in add_classes in app/bootstrap.php

Autoloader::add_classes(array( 'Response' => APPPATH.'classes/response.php', ));

However, if you try to extend Autoloader using the same steps, it won't work.
Why? Because Autoloader is an exception!

What I struggled with

the document above it says:

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 I mean

 

  1. Manually require the Autoloader class file that extends FuelCoreAutoloader in app/bootstrap.php
  2. remove the line that aliases the core class to global (???)

 

I understand the first one! Here's what you should do

Create app/classes/autoloader.php and

class Autoloader extends FuelCoreAutoloader { }

At the beginning of app/bootatrap.php

require APPPATH.'classes'.DIRECTORY_SEPARATOR.'autoloader.php';

Described below

 

I didn't understand the second part! I ignored it and opened the page

Cannot redeclare class Autoloader

I was worried about this for a while but I solved it

In conclusion, around line 50 of public/index.php

class_alias('Fuel\Core\Autoloader', 'Autoloader');

Just comment this out and you'll be fine

 

What this line means is

"Alias ​​the Autoloader in the global namespace to the Autoloader in the FuelCore namespace."

(For more information about class_alias(), see http://php.net/manual/function.class-alias.php

Because of this alias,

class Autoloader extends FuelCoreAutoloader { }

This resulted in Autoloader being defined twice and inheriting itself

summary

When extending the core class Autoloader

  1. Create app/classes/autoloader.php
    class Autoloader extends FuelCoreAutoloader { }
  2. at the beginning of app/bootstrap.php
    require APPPATH.'classes'.DIRECTORY_SEPARATOR.'autoloader.php';

    Describe

  3. public/index.php (around line 50)
    class_alias('Fuel\Core\Autoloader', 'Autoloader');

    Comment out

If you keep these three points in mind, you'll be fine

 

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
2,268
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author