如何使用composer autoloader实现继承?

How to implement inheritance using composer autoloader?

我有 2 个 class,每个都有 2 个方法。此外,我在所有 classes.

的所有方法之间都有一些常量数据

所以我使用了包含那些常量数据的父 class 然后我扩展了父 class 的其他 classes(用于访问所有常量数据所有 classes).

的方法

这是我的结构:github demo

// c:\xampp\htdocs\myweb\classes

// ---------------------------------------------------- Father.php
  class Father{
    protected $arr1 = array("there is some data in here");
    protected $arr2 = array("there is some data in here");
    protected $arr3 = array("there is some data in here");
  }


// ---------------------------------------------------- Autoloader.php
  function my_autoloader($class) {
      require_once($class.".php");
  }
  spl_autoload_register('my_autoloader');


// ---------------------------------------------------- Child1.php
  class Child1 extends Father{
    public function func1(){// using of those array in this method}
    public function func2(){// using of those array in this method}
  }



// ---------------------------------------------------- Child2.php
  class Child2 extends Father{
    public function func1(){// using of those array in this method}
    public function func2(){// using of those array in this method}
  }

现在我想知道,如何使用 composer autoloader 实现我的代码?

真正的问题是:'How do I use composer?'

https://www.codementor.io/php/tutorial/composer-install-php-dependency-manager

因此,当您 运行 完成上述内容后,考虑到您的问题中的当前项目:

  • 你会把你的自动加载器拿出来,因为这一切都将由作曲家为你生成的文件处理。事实上,如果您当前将所有 php 文件都放在同一个目录中,那么您当前不需要自动加载器。

  • 您会 include 在 bootstrap 文件中生成的作曲家自动加载器,该文件会在项目的每个页面 运行 上加载。

  • 你会给每个 class 一个自己的文件,就像你在这里所做的那样。这是坚持psr的一部分。您将使用 adhe运行ce 设置一些命名空间到 psr-4.

  • 如果您有一个旧的遗留项目,如果不可能重命名每个 class 以符合 psr,您将生成 classmap 完全,这将适用于一个文件中的多个 classes。但是,在大多数情况下,从一开始就使用 psr 命名标准是最佳选择。