在PHP中调用来自不同文件的类

I am trying to call a class from file. I used the code below:

<?php
use Fieg\Bayes\Classifier;
use Fieg\Bayes\Tokenizer\WhitespaceAndPunctuationTokenizer;

$tokenizer = new WhitespaceAndPunctuationTokenizer();
$classifier = new Classifier($tokenizer);

$classifier->train('en', 'This is english');
$classifier->train('fr', 'Je suis Hollandais');

$result = $classifier->classify('This is a naive bayes classifier');

But it gives error:

Fatal error: Class 'Fieg\Bayes\Tokenizer\WhitespaceAndPunctuationTokenizer' not found in C:\xampp\htdocs\Nayve\test.php on line 5

My folder location is:

enter image description here

And my code in WhitespaceAndPunctuationTokenizer class is :

<?php

/*
 * @author Jeroen Fiege <jeroen@webcreate.nl>
 * @copyright Webcreate (http://webcreate.nl)
 */

namespace Fieg\Bayes\Tokenizer;

use Fieg\Bayes\TokenizerInterface;

class WhitespaceAndPunctuationTokenizer implements TokenizerInterface
{
    protected $pattern = "/[ ,.?!-:;\
\\\t…_]/u";

    public function tokenize($string)
    {
        $retval = preg_split($this->pattern, mb_strtolower($string, 'utf8'));
        $retval = array_filter($retval, 'trim');
        $retval = array_values($retval);

        return $retval;
    }
}

define the include path so php can find it..

set_include_path(get_include_path() . PATH_SEPARATOR . 'path/to/classes');

try the following:

use Fieg\Bayes\Tokenizer\WhitespaceAndPunctuationTokenizer as WhitespaceTokenizer;

and then use this to create your object:

$tokenizer = new WhitespaceTokenizer();

It looks like you are using composer so, if you're loading your file directly, you'll need to include the composer autoloader bootstrap file.

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

// All your code goes here

If you don't have a vendor folder or haven't setup your composer.json configuration, then you'll need to make sure your composer.json file contains:

{
    "autoload": {
        "psr-4": {"Fieg\\Bayes\\": "Fieg/Bayes/"}
    }
}

And then run a composer install to setup your autoloading files:

$ composer install

Reference: https://getcomposer.org/doc/01-basic-usage.md#autoloading