Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

php - 在Composer自动加载中定义加载类的顺序(Define order of loading classes in Composer autoload)

I'm using psr4 autoloading to load all classes from a directory, below is what I've done.

(我正在使用psr4自动加载来从目录加载所有类,以下是我所做的事情。)

autoload": {
    "psr-4": {
      "jframework\":"vendor/myapp/src/"
    }
 }

is there a way, I can load files orderly eg if I want to load in this order

(有没有办法,我可以按顺序加载文件,例如,如果我想以此顺序加载)

Bootstrap.php
Core.php
Session.php
Router.php
  ask by Juliver Galleto translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The whole principle of autoload is that files are looked up when needed by code.

(自动加载的全部原理是在代码需要时查找文件。)

It also assumes that class files don't have any runtime logic and including class file only makes class definition(s) available.

(它还假定类文件没有任何运行时逻辑,并且包括类文件使类定义可用。)

If you have some kind of runtime logic with definitions it's advisable to separate it out for use with autoload.

(如果您有某种带有定义的运行时逻辑,建议将其分离出来以用于自动加载。)

You could try to make an explicit load procedure that tries to instantiate classes in your preferred order but that won't be too robust long term.

(您可以尝试创建一个显式的加载过程,该过程尝试按您的首选顺序实例化类,但长期来看不会太健壮。)

In a nutshell — change to autoload completely or stick with explicit load, don't mix the approaches for the same classes.

(简而言之,更改为完全自动加载或坚持显式加载,不要将相同类的方法混在一起。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...