Apache报Parse error: syntax error, unexpected 'class'错误求助
Alright, let's tackle that 'unexpected T_CLASS' error you're hitting. These syntax errors can be tricky even when you’ve copied code straight from a tutorial, but they almost always boil down to a few common culprits. Here’s your step-by-step troubleshooting plan:
1. Verify Your PHP Version
First, let’s rule out version compatibility. While basic class definitions work in PHP 5.0+, older Mac systems sometimes ship with outdated PHP versions (or you might have an older version configured in Apache/XAMPP).
- To check your web server’s PHP version: Create a file named
phpinfo.phpin your web root with this content:
Access it via your browser (e.g.,<?php phpinfo(); ?>http://localhost/phpinfo.php) and look for the "PHP Version" line. - To check your CLI PHP version: Open Terminal and run
php -v.
If you’re running PHP 4.x (unlikely but possible on very old systems), you’ll need to upgrade—PHP 4 doesn’t support modern OOP syntax. For PHP 5.0+, move to the next steps.
2. Check for Invisible Characters or UTF-8 BOM
This is the most common fix for this exact error. If your Article.php file starts with a UTF-8 Byte Order Mark (BOM) or invisible whitespace before the <?php tag, PHP will interpret that as output, throwing off the parser when it hits the class keyword.
- Open
Article.phpin a code editor like VS Code or Sublime Text. - Check the file encoding: In VS Code, look at the bottom-right corner—if it says "UTF-8 with BOM", click it and select "Save with UTF-8" (without BOM).
- Ensure the
<?phptag is the very first thing in the file—no spaces, newlines, or weird hidden characters before it.
3. Audit Included/Required Files for Unclosed Tags
If Article.php is being included or required by another file (like index.php or a config file), check those parent files for issues:
- Look for unclosed
?>tags at the end of parent files. Any whitespace or newlines after?>will be sent as output, causing the same parsing error inArticle.php. - Best practice: Omit the closing
?>tag entirely in PHP files—this eliminates the risk of accidental output after the tag.
4. Simplify the File to Isolate the Issue
Let’s narrow down whether the problem is in the full code or a hidden formatting issue. Replace all content in Article.php with this minimal test code:
<?php class Article { public $id = null; }
If this still throws the same error, the problem is with the file itself (encoding, invisible characters) or your PHP environment. If it works, the issue is in the rest of the code you copied (e.g., a missing closing comment bracket or syntax error later in the file that’s causing the parser to choke early).
5. Check for Typos or Malformed Comments
Even with copied code, it’s easy to get a stray character or malformed comment. Double-check the lines just before the class definition:
<?php /** * Class to handle articles */ class Article {
Make sure the opening <?php is correct, the comment block is properly closed with */, and there are no weird characters (like full-width spaces or curly quotes) mixed in.
内容的提问来源于stack exchange,提问作者Livia Rett

