Search code examples
phpnamespaces

PHP Using a Library via Composer To An Existing PHP App That Don't Use Composer


I have an existing php app. I would like to use this library: Name Parser This library used composer. I download the library via composer and copied the NameParser folder into my existing PHP App.

When I used it I got an error: PHP Fatal error: Uncaught Error: Class "HumanNameParser\Name" ... /HumanNameParser/Parser.php:88

How I used the library in a .php file

<?php
  include_once('includes/HumanNameParser/Parser.php');
  $nameparser = new Parser();
?>

I'm pretty sure, I am messing this up. I don't know the difference between namespace, use, include_once(), include(), require(), require_once().


Solution

  • Run composer require davidgorges/human-name-parser.

    Then in your application:

    include_once('vendor/autoload.php');

    Then you'll be able to use it with:

    new HumanNameParser\Parser\Parser();
    

    You can add the autoloader to your entire application (where it bootstraps) or just to the file where you want to use the Parser package.