0

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

3
  • Can you post the top of bitm/person/person.php? Commented Dec 12, 2015 at 16:57
  • Take a look at getcomposer.org/doc/01-basic-usage.md#autoloading and php-fig.org/psr/psr-4 Commented Dec 12, 2015 at 16:57
  • By careffully reading these small paragraphs and trying to apply them in your project, you'll solve these problems ;) Commented Dec 12, 2015 at 16:59

1 Answer 1

1

If I read your ASCII-art correctly, you have four errors:

  1. The 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.
  2. The PSR-4 prefix has to end with a backslash. Composer might already have complained about this. Run 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\\".
  3. The PSR-4 standard tells you that the prefix part is removed from the classname, and the remainder is converted into a path. If you have a class 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).
  4. Although PHP classes are not case sensitive, PHP will not convert the class names cases when autoloading, and the class name will be transformed into a file name. But file systems are case sensitive (unless you are using Windows). You have a directory 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.

Sign up to request clarification or add additional context in comments.

1 Comment

thnx bro! I was confused little bit about composer.your assistance really appreciated :)

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.