0

I am using Composer version 2.2.25

Per https://getcomposer.org/doc/05-repositories.md#path, I have this structure:

├── path
│   └── to
│       └── composer_stuff
│           └── compiled
│               └── composer.json
│           └── local
│               └── composer.json
│               └── src
│                   └── Foo
│                       └── Foo.php
│       └── public_html
│           └── index.php

/path/to/composer_stuff/compiled/composer.json

{
    "repositories": [
        {
            "type": "path",
            "url": "/path/to/composer_stuff/local",
            "options": {
                "symlink": true
            }
        }
    ],
    "require": {
        "local/local": "dev-master"
    }
}

/path/to/composer_stuff/local/composer.json

{
    "name": "local/local",
    "type": "library",
    "autoload": {
        "psr-4": {
            "local\\local\\": "src/"
        }
    }
}

/path/to/composer_stuff/local/src/Foo/Foo.php

<?php
use local\local\Foo;

class Foo {
    public function __construct() {}
}

/path/to/public_html/index.php

<?php
require_once( '/path/to/composer_stuff/compiled/vendor/autoload.php' );

$foo = new local\local\Foo\Foo();

Commands:

cd /path/to/composer_stuff/compiled
composer update

vendor directory was created successfully with expected symlink.


Why does my index.php page result in this error?

Fatal error: Cannot declare class Foo because the name is already in use in /path/to/composer_stuff/local/src/Foo/Foo.php on line 4

To note: it doesn't matter if I implement something totally random like fgjhrtjhfgjghjg\fgjhrtjhfgjghjg(), same error.

I am at my wits end here =(

5
  • Did you run composer init or composer install? I do not see the vendor directory. Commented Oct 23 at 13:11
  • @MarkusZeller Yes. I can update my question to reflect that. Commented Oct 23 at 13:15
  • Out of curiosity: is there any good reason to use Composer's LTS version, and not a more recent one? Commented 2 days ago
  • @NicoHaase Yes, per packagist.org/packages/composer/composer#2.2.25 the requirement is php: ^5.3.2 || ^7.0 || ^8.0 Commented yesterday
  • And what's the reason now? The most recent Composee version is compatible with PHP >= 7.2.5, and I hope you're not running any older version? Commented yesterday

1 Answer 1

1

The statement use local\local\Foo means importing a class named Foo, so you cannot redeclare a class with the same name.

Have you confused importing a class with declaring a namespace?

<?php
namespace local\local;

class Foo {
    public function __construct() {}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Have I confused something? I don't know. I've been trying to piece this together with examples from packages like PHPMailer and PHPSpreadSheet. Regardless, your suggestion results in Fatal error: Class 'local\local\Foo\Foo' not found in /path/to/public_html/index.php on line 4. Even changing it to $foo = new local\local\Foo(); results in Fatal error: Class 'local\local\Foo' not found in /path/to/public_html/index.php on line 4
OH MY GAWD. I did confuse use and namespace...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.