I got stuck in implementation of composer. I don't understand the autoload formation. I am going to sketch the file directory then you people just tell me how I formed the destination of a class into autoload:
|---------src/
| |-----bitm/
| | |---person/
|person.php
| | |---Age/
|age.php
| |-----vendor/
|----composer
|autoload.php
| |
| |composer.json
| |
| |index.php
above structure is my folder structure in web server.
following code is for composer.json
{
"autoload":{
"psr-4":{
"bitm\\person" : "src"
}
}}
following code is for index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Greeting</title>
</head>
<body>
<?php
/*function __autoload($className){
//var_dump($className);
include_once($className.".php");
}*/
include_once("vendor/autoload.php");
use bitm\person\person;
use bitm\age\age;
$mamun=new person('Mamun');
$age=new age(24);
$mamun->greeting();
$age->personAge();
?>
</body>
</html>
I used namespace for person.php is bitm\\person and I used namespace for age.php is bitm\\age.
my question is to you guys why it shows
Fatal error: Class 'bitm\\person\\person' not found in C:\\xampp\\htdocs\\basis_mamun_ewu\\Mamun\\src\\index.php on line 18
If I read your ASCII-art correctly, you have four errors:
composer.json
is located inside the src
directory. That's OK, but any paths related to autoloading are relative to the position of the composer.json
file, and you have src
in your autoloading path - wrong. The code you want to load is in the bitm
directory. composer validate
to see this and probably more errors. As you already did with the inner backslash, it has to be escaped in JSON, so it should read "bitm\\\\person\\\\"
. bitm\\person\\person
and a prefix bitm\\person
, then the remainder of that classname is person
, will be converted into the path person.php
and assumed in the directory you gave in the composer.json
file. I already mentioned that src
was wrong, but as an example: Composer would try to load the file src/person.php
(relative to the position of composer.json
). Age
, but are using the namespace age
. This will not match. Always use the same case everywhere. Also note that you made a prefix for bitm\\person
, but not for bitm\\age
, so you cannot autoload the age classes. You can have multiple prefixes in the autoloading section, or use a more general bitm
prefix that will catch all classes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.