This package is abandoned and no longer maintained. The author suggests using the phpdocumentor/type-resolver package instead.

Library to extract PHP class name from source file

v1.0.0 2018-08-16 01:19 UTC

This package is auto-updated.

Last update: 2023-06-09 12:03:28 UTC


Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight BCH compliance Packagist Stable Version Packagist License

Library to extract fully qualified class name (FQCN) from a PHP source file.

Caveat; only considers one class per source file.


composer require jasny/fqcn-reader


Single source file

The FQCNReader allows extracting a class name from a PHP source file.

use Jasny\FQCN\FQCNReader;

$reader = new FQCNReader();

$class = $reader->getClass("path/to/source.php");


The FQCNIterator is an OuterIterator, meaning it will iterate over an iterator applying logic. The iterator expects to traverse over source files.

With the file names in an array, use ArrayIterator.

use Jasny\FQCN\FQCNIterator;

$sourceFiles = glob('path/to/directory/*.php');
$sourceIterator = new ArrayIteractor($sourceFiles);

$fqcnIterator = new FQCNIterator($sourceIterator);

foreach ($fqcnIteractor as $file => $class) {
   // do something with $class

Alternatively use SPL Iterators like DirectoryIterator, RecursiveDirectoryIterator or GlobIterator.

use Jasny\FQCN\FQCNIterator;

$directoryIterator = new RecursiveDirectoryIterator('path/to/project/');
$recursiveIterator = new RecursiveIteratorIterator($directoryIterator);
$sourceIterator = new RegexIterator($recursiveIterator, '/^.+\.php$/i', RegexIterator::GET_MATCH);

$fqcnIterator = new FQCNIterator($sourceIterator);

foreach ($fqcnIteractor as $file => $class) {
   // do something with $class

Files that do not define a class are skipped.