1

I created my first PHP library to host on GitHub.

https://github.com/TBETool/password-generator

on installing the library in my PHP Application using composer, the directory structure is like vendor

|- tbetool
   |- php-password-generator
      |- src
         |- PasswordGenerator.php
      |- README.md
      |- composer.json

Content of composer.json is

{
    "name": "tbetool/php-password-generator",
    "type": "library",
    "homepage": "http://thebornengineer.com",
    ],
    "require": {
        "php": ">=5.3.0"
    }
}

content of PasswordGenerator.php

<?php
namespace TBETool;

/**
 * Class PasswordGenerator
 * @package TBETool
 */
class PasswordGenerator
{
   // content goes here
}

and entry in my project's composer.json at /composer.json

"require": {
    "php": ">=5.6",
    .... other packages....
    "tbetool/php-password-generator": "^1.2"
},

But when I use in my program.

<?php

use TBETool\PasswordGenerator;
class MyClass {
   function myFun() {
       $password = new PasswordGenerator();
   }
}

But it gives error as

Class 'TBETool\PasswordGenerator' not found

Is there something error with setting up my library to be used using composer?

1
  • 1
    I've never written a library but I presume you need an autoload section somewhere in your composer.json file. How do you load classes when using it locally? Commented Nov 3, 2017 at 11:47

1 Answer 1

4

You should use a psr-4 autoloading in your composer.json file

"autoload": {
    "psr-4": {
        "Namespace\\": "root/path/for/namespace"
    }
}

then dump your autoload composer dump-autoload

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

Comments

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.