Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:29:56+00:00 2026-05-24T23:29:56+00:00

I have a php file which contains only one class. how can I know

  • 0

I have a php file which contains only one class. how can I know what class is there by knowing the filename? I know I can do something with regexp matching but is there a standard php way? (the file is already included in the page that is trying to figure out the class name).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:29:57+00:00Added an answer on May 24, 2026 at 11:29 pm

    There are multiple possible solutions to this problem, each with their advantages and disadvantages. Here they are, it’s up to know to decide which one you want.

    Tokenizer

    This method uses the tokenizer and reads parts of the file until it finds a class definition.

    Advantages

    • Do not have to parse the file entirely
    • Fast (reads the beginning of the file only)
    • Little to no chance of false positives

    Disadvantages

    • Longest solution

    Code

    $fp = fopen($file, 'r');
    $class = $buffer = '';
    $i = 0;
    while (!$class) {
        if (feof($fp)) break;
    
        $buffer .= fread($fp, 512);
        $tokens = token_get_all($buffer);
    
        if (strpos($buffer, '{') === false) continue;
    
        for (;$i<count($tokens);$i++) {
            if ($tokens[$i][0] === T_CLASS) {
                for ($j=$i+1;$j<count($tokens);$j++) {
                    if ($tokens[$j] === '{') {
                        $class = $tokens[$i+2][1];
                    }
                }
            }
        }
    }
    

    Regular expressions

    Use regular expressions to parse the beginning of the file, until a class definition is found.

    Advantages

    • Do not have to parse the file entirely
    • Fast (reads the beginning of the file only)

    Disadvantages

    • High chances of false positives (e.g.: echo "class Foo {";)

    Code

    $fp = fopen($file, 'r');
    $class = $buffer = '';
    $i = 0;
    while (!$class) {
        if (feof($fp)) break;
    
        $buffer .= fread($fp, 512);
        if (preg_match('/class\s+(\w+)(.*)?\{/', $buffer, $matches)) {
            $class = $matches[1];
            break;
        }
    }
    

    Note: The regex can probably be improved, but no regex alone can do this perfectly.

    Get list of declared classes

    This method uses get_declared_classes() and look for the first class defined after an include.

    Advantages

    • Shortest solution
    • No chance of false positive

    Disadvantages

    • Have to load the entire file
    • Have to load the entire list of classes in memory twice
    • Have to load the class definition in memory

    Code

    $classes = get_declared_classes();
    include 'test2.php';
    $diff = array_diff(get_declared_classes(), $classes);
    $class = reset($diff);
    

    Note: You cannot simply do end() as others suggested. If the class includes another class, you will get a wrong result.


    This is the Tokenizer solution, modified to include a $namespace variable containing the class namespace, if applicable:

    $fp = fopen($file, 'r');
    $class = $namespace = $buffer = '';
    $i = 0;
    while (!$class) {
        if (feof($fp)) break;
    
        $buffer .= fread($fp, 512);
        $tokens = token_get_all($buffer);
    
        if (strpos($buffer, '{') === false) continue;
    
        for (;$i<count($tokens);$i++) {
            if ($tokens[$i][0] === T_NAMESPACE) {
                for ($j=$i+1;$j<count($tokens); $j++) {
                    if ($tokens[$j][0] === T_STRING) {
                         $namespace .= '\\'.$tokens[$j][1];
                    } else if ($tokens[$j] === '{' || $tokens[$j] === ';') {
                         break;
                    }
                }
            }
    
            if ($tokens[$i][0] === T_CLASS) {
                for ($j=$i+1;$j<count($tokens);$j++) {
                    if ($tokens[$j] === '{') {
                        $class = $tokens[$i+2][1];
                    }
                }
            }
        }
    }
    

    Say you have this class:

    namespace foo\bar {
        class hello { }
    }
    

    …or the alternative syntax:

    namespace foo\bar;
    class hello { }
    

    You should have the following result:

    var_dump($namespace); // \foo\bar
    var_dump($class);     // hello
    

    You could also use the above to detect the namespace a file declares, regardless of it containing a class or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php file which I will be using as exclusively as an
I have an index.php file which has to process many different file types. How
This is driving me crazy. I have this one php file on a test
I have a php file which is called every X second in ajax to
Ok. I have my query written finally for my php file (which will write
i have a php file launching my exe. the exe does cout and the
I have a PHP file, Test.php, and it has two functions: <?php echo displayInfo();
I have a PHP file and an image in the same directory. How could
In php I have open a .php file and want to evaluate certain lines.
As many do I have a config.php file in the root of a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.