【问题标题】:How to implement inheritance using composer autoloader?如何使用composer autoloader实现继承?
【发布时间】:2015-10-24 12:04:07
【问题描述】:

我有 2 个类,每个类都有 2 个方法。此外,我在所有类的所有方法之间都有一些常量数据。

所以我使用了一个包含这些常量数据的父类,然后我扩展了父类的其他类(用于访问所有类的所有方法中的常量数据)

这是我的结构: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 实现我的代码?

【问题讨论】:

  • 你想要psr-0/4的文档吗?
  • @Federico 我想使用作曲家。
  • 我不明白,你要使用composer的自动加载器吗?还是什么?
  • @Federico 是的,我想使用 composer 的自动加载器

标签: php oop composer-php autoloader


【解决方案1】:

真正的问题是:“我如何使用作曲家?”​​p>

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

因此,当您完成该操作时,请考虑您当前的问题:

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

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

  • 您应该为每个班级提供自己的文件,就像您在此处所做的那样。这是遵守psr 的一部分。你会根据psr-4 设置一些命名空间。

  • 1234563但是,在大多数情况下,从一开始就使用psr 命名标准是最佳选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多