【问题标题】:PHP - Extend classes using composer autoloader causes redeclare errorPHP - 使用 Composer 自动加载器扩展类会导致重新声明错误
【发布时间】:2016-09-09 03:49:36
【问题描述】:

我只是在学习如何在自己的课程中使用作曲家。 所以我有这个目录结构

我使用的是 PHP 5.5.30 和 Composer 1.1.0

  • PDF
    • test.php
    • composer.json
    • 供应商/
      • 自动加载.php
      • jarouche/
        • jarouche.php
        • jarouche2.php

所以,我有

  test.php
  <?php
      require_once('vendor/autoload.php');
      use jarouche\jarouche2;

      $teste = new jarouche2();
      $teste->teste();
   ?>

   jarouche.php

   <?php

       namespace jarouche;

       class jarouche{

           public function teste(){
               echo 'jarouche';
           }

        }
   ?>

  jarouche2.php

   <?php

       namespace jarouche;

       class jarouche2 extends jarouche{

           public function teste(){
               echo 'jarouche2';
           }

        }
   ?>

  composer.json

  {
    ...

          "autoload": {
                        ...
                        "psr-4": {"jarouche\\": "vendor/jarouche"
           }
   }

But, when I run test.php I got this error "Fatal error: Cannot redeclare class jarouche\jarouche2 in C:\xampp\htdocs\Pdf\vendor\jarouche\jarouche2.php on line 6"

我尝试过添加if (!class_exists('MyClass')) in jarouche2.php,尝试更新作曲家...没有任何效果。

我的错误是什么?

【问题讨论】:

  • Pdf\vendor\jarouche\jarouche2.php 不是app\vendor\jarouche\jarouche2.php
  • 感谢您的建议。我已经解决了!

标签: php inheritance composer-php autoloader


【解决方案1】:

出于某种原因,我做了一个作曲家自我更新,现在它正在工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 2018-03-12
    • 2012-12-24
    • 2010-10-17
    • 2014-11-07
    相关资源
    最近更新 更多