Автозагрузка PHP Composer не работает, классы не инициированы

#php #composer-php

#php #композитор-php

Вопрос:

Я пытаюсь использовать Composer в PHP. Я установил composer в свой каталог проекта, затем установил некоторые необходимые классы / методы PHP с помощью cli:

 php composer.phar require ahsankhatri/firestore-php
php composer.phar require guzzlehttp/guzzle
  

Отлично, это сработало отлично!

После успешной установки я оптимизировал файл блокировки с помощью:

 php composer.phar dump-autoload --optimize
  

добавлен 101 класс (cli вернул это сообщение).

Теперь я добавил автозагрузчик в свой PHP-файл, я думал, это будет легко… просто добавьте autoload.php

 <?php
require 'vendor/autoload.php';

//print_r(get_declared_classes());

$firestoreClient = new FirestoreClient('Dimo-233713', '
AIzaSyCNHz_EbF13KuAvEnlBIto_2n1eIqkCnzk', [
    'database' => 'test',
]);
  

Но, когда я запускаю этот скрипт на своем XAMPP, в журнале PHP говорится:

 [28-Mar-2019 13:27:33 Europe/Berlin] PHP Fatal error:  Uncaught Error: Class 'FirestoreClient' not found in /Users/***/Documents/Websites/xampp/domare.nl/htdocs/subdomains/portal/firestore.php:6
Stack trace:
#0 {main}
  thrown in /Users/***/Documents/Websites/xampp/domare.nl/htdocs/subdomains/portal/firestore.php on line 6
  

Класс не найден. Я сбросил все классы, как вы можете видеть в прокомментированной строке. Но класс не загружен, который я запросил с помощью composer. Нет ни одного из 101 класса…

Что здесь не так?

Работа с PhpStorm. Работает на XAMPP с PHP7.2.4

И вот файл Composer.json

 {
    "require": {
        "guzzlehttp/guzzle": "^6.3",
        "ahsankhatri/firestore-php": "^2.0"
    }
}
  

И это композитор.заблокировать файл json

 {
    "_readme": [
        "This file locks the dependencies of your project to a known state",
        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
        "This file is @generated automatically"
    ],
    "content-hash": "caedd171718e9452e0e742dcd84f2235",
    "packages": [
        {
            "name": "ahsankhatri/firestore-php",
            "version": "v2.0.1",
            "source": {
                "type": "git",
                "url": "https://github.com/ahsankhatri/firestore-php.git",
                "reference": "ecdd92bdcb3f00c0b94e0e1909ac104fe20b1eb8"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/ahsankhatri/firestore-php/zipball/ecdd92bdcb3f00c0b94e0e1909ac104fe20b1eb8",
                "reference": "ecdd92bdcb3f00c0b94e0e1909ac104fe20b1eb8",
                "shasum": ""
            },
            "require": {
                "ext-curl": "*",
                "ext-json": "*",
                "guzzlehttp/guzzle": "~6.0|~5.0|~4.0",
                "php": ">=5.6.6"
            },
            "require-dev": {
                "phpunit/phpunit": "5.7"
            },
            "type": "library",
            "autoload": {
                "psr-4": {
                    "MrShan0\PHPFirestore\": "src"
                }
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "MIT"
            ],
            "authors": [
                {
                    "name": "Ahsaan Muhammad Yousuf",
                    "email": "ahsankhatri1992@gmail.com",
                    "homepage": "https://ahsaan.me",
                    "role": "Developer"
                }
            ],
            "description": "Firestore PHP Client",
            "homepage": "https://github.com/ahsankhatri/firestore-php",
            "keywords": [
                "firebase",
                "firestore",
                "google",
                "php"
            ],
            "time": "2019-02-25T13:27:14 00:00"
        },
        {
            "name": "guzzlehttp/guzzle",
            "version": "6.3.3",
            "source": {
                "type": "git",
                "url": "https://github.com/guzzle/guzzle.git",
                "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/guzzle/guzzle/zipball/407b0cb880ace85c9b63c5f9551db498cb2d50ba",
                "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba",
                "shasum": ""
            },
            "require": {
                "guzzlehttp/promises": "^1.0",
                "guzzlehttp/psr7": "^1.4",
                "php": ">=5.5"
            },
            "require-dev": {
                "ext-curl": "*",
                "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0",
                "psr/log": "^1.0"
            },
            "suggest": {
                "psr/log": "Required for using the Log middleware"
            },
            "type": "library",
            "extra": {
                "branch-alias": {
                    "dev-master": "6.3-dev"
                }
            },
            "autoload": {
                "files": [
                    "src/functions_include.php"
                ],
                "psr-4": {
                    "GuzzleHttp\": "src/"
                }
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "MIT"
            ],
            "authors": [
                {
                    "name": "Michael Dowling",
                    "email": "mtdowling@gmail.com",
                    "homepage": "https://github.com/mtdowling"
                }
            ],
            "description": "Guzzle is a PHP HTTP client library",
            "homepage": "http://guzzlephp.org/",
            "keywords": [
                "client",
                "curl",
                "framework",
                "http",
                "http client",
                "rest",
                "web service"
            ],
            "time": "2018-04-22T15:46:56 00:00"
        },
        {
            "name": "guzzlehttp/promises",
            "version": "v1.3.1",
            "source": {
                "type": "git",
                "url": "https://github.com/guzzle/promises.git",
                "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646",
                "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646",
                "shasum": ""
            },
            "require": {
                "php": ">=5.5.0"
            },
            "require-dev": {
                "phpunit/phpunit": "^4.0"
            },
            "type": "library",
            "extra": {
                "branch-alias": {
                    "dev-master": "1.4-dev"
                }
            },
            "autoload": {
                "psr-4": {
                    "GuzzleHttp\Promise\": "src/"
                },
                "files": [
                    "src/functions_include.php"
                ]
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "MIT"
            ],
            "authors": [
                {
                    "name": "Michael Dowling",
                    "email": "mtdowling@gmail.com",
                    "homepage": "https://github.com/mtdowling"
                }
            ],
            "description": "Guzzle promises library",
            "keywords": [
                "promise"
            ],
            "time": "2016-12-20T10:07:11 00:00"
        },
        {
            "name": "guzzlehttp/psr7",
            "version": "1.5.2",
            "source": {
                "type": "git",
                "url": "https://github.com/guzzle/psr7.git",
                "reference": "9f83dded91781a01c63574e387eaa769be769115"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/guzzle/psr7/zipball/9f83dded91781a01c63574e387eaa769be769115",
                "reference": "9f83dded91781a01c63574e387eaa769be769115",
                "shasum": ""
            },
            "require": {
                "php": ">=5.4.0",
                "psr/http-message": "~1.0",
                "ralouphie/getallheaders": "^2.0.5"
            },
            "provide": {
                "psr/http-message-implementation": "1.0"
            },
            "require-dev": {
                "phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.8"
            },
            "type": "library",
            "extra": {
                "branch-alias": {
                    "dev-master": "1.5-dev"
                }
            },
            "autoload": {
                "psr-4": {
                    "GuzzleHttp\Psr7\": "src/"
                },
                "files": [
                    "src/functions_include.php"
                ]
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "MIT"
            ],
            "authors": [
                {
                    "name": "Michael Dowling",
                    "email": "mtdowling@gmail.com",
                    "homepage": "https://github.com/mtdowling"
                },
                {
                    "name": "Tobias Schultze",
                    "homepage": "https://github.com/Tobion"
                }
            ],
            "description": "PSR-7 message implementation that also provides common utility methods",
            "keywords": [
                "http",
                "message",
                "psr-7",
                "request",
                "response",
                "stream",
                "uri",
                "url"
            ],
            "time": "2018-12-04T20:46:45 00:00"
        },
        {
            "name": "psr/http-message",
            "version": "1.0.1",
            "source": {
                "type": "git",
                "url": "https://github.com/php-fig/http-message.git",
                "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
                "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
                "shasum": ""
            },
            "require": {
                "php": ">=5.3.0"
            },
            "type": "library",
            "extra": {
                "branch-alias": {
                    "dev-master": "1.0.x-dev"
                }
            },
            "autoload": {
                "psr-4": {
                    "Psr\Http\Message\": "src/"
                }
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "MIT"
            ],
            "authors": [
                {
                    "name": "PHP-FIG",
                    "homepage": "http://www.php-fig.org/"
                }
            ],
            "description": "Common interface for HTTP messages",
            "homepage": "https://github.com/php-fig/http-message",
            "keywords": [
                "http",
                "http-message",
                "psr",
                "psr-7",
                "request",
                "response"
            ],
            "time": "2016-08-06T14:39:51 00:00"
        },
        {
            "name": "ralouphie/getallheaders",
            "version": "2.0.5",
            "source": {
                "type": "git",
                "url": "https://github.com/ralouphie/getallheaders.git",
                "reference": "5601c8a83fbba7ef674a7369456d12f1e0d0eafa"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/5601c8a83fbba7ef674a7369456d12f1e0d0eafa",
                "reference": "5601c8a83fbba7ef674a7369456d12f1e0d0eafa",
                "shasum": ""
            },
            "require": {
                "php": ">=5.3"
            },
            "require-dev": {
                "phpunit/phpunit": "~3.7.0",
                "satooshi/php-coveralls": ">=1.0"
            },
            "type": "library",
            "autoload": {
                "files": [
                    "src/getallheaders.php"
                ]
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "MIT"
            ],
            "authors": [
                {
                    "name": "Ralph Khattar",
                    "email": "ralph.khattar@gmail.com"
                }
            ],
            "description": "A polyfill for getallheaders.",
            "time": "2016-02-11T07:05:27 00:00"
        }
    ],
    "packages-dev": [],
    "aliases": [],
    "minimum-stability": "stable",
    "stability-flags": [],
    "prefer-stable": false,
    "prefer-lowest": false,
    "platform": [],
    "platform-dev": []
}
  

Комментарии:

1. Можете ли вы поделиться composer.json файлом?

2. @manuerumx отклонился от курса!! Я добавлю это в сообщение! Заранее спасибо.

3. В вашем первом примере вы забыли use инструкцию: use MrShan0PHPFirestoreFirestoreClient;

4. @xabbuh спасибо, но… Нет, это добавляет класс только для нескольких методов только в этом классе. Мне нужны все зависимости …. из всех 101 добавленных классов в composer, это то, для чего предназначен Composer? У кого-нибудь есть решение?

5. Нет, это не то, что делает автозагрузчик. Вам по-прежнему необходимо преобразовать такие имена, как FirestoreClient , в их полное имя MrShan0PHPFirestoreFirestoreClient , либо написав его по буквам, либо используя use инструкцию. Автозагрузчик будет require_once получать только нужный файл для этого класса, когда вы его используете.