|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-02-26 11:00 UTC] thomas at stauntons dot org
Description:
------------
I am writing a class that will include another file
containing a class that needs to implement a specific
interface. When calling php_check_syntax on the file its
behavious differs depending on whether or not the class
implements my interface. If the file implements my
interface the class will not show up in
get_declared_classes() and an include() of the file will
work, but if the class doesn't implement my interface the
class will be in get_declared_classes() and the include
will fail with 'cannot redeclareclass'
Reproduce code:
---------------
in Main.php
<?php
interface MustImplement {}
if (!php_check_syntax('includeme.php'))
die('Bad Syntax\n');
else include('includeme.php');
?>
in includeme.php (Case 1)
<?php
class AClass implements MustImplement
{}
?>
in includeme.php (Case 2)
<?php
class AClass
{}
?>
Expected result:
----------------
Case 1 of includeme.php will work OK, php_syntax_check will
succeed and include will load the file OK, AClass will be
available.
Case 2 will fail, php_syntax_check will work but include
will fail with 'cannot redeclare class' & Illegal
Instruction
Actual result:
--------------
Just as Above.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 00:00:02 2025 UTC |
Bogus? Could someone document this function so we know what the "proper" usage is? Is this funtion meant to load the file into the current scope as it's syntax is checked? If so, please say so in the documentation. Otherwise, I have another bug report to file. original.php <?php $bool = php_check_syntax('checkme.php'); foo(); $bar = new Bar; $bar->foo(); ?> checkme.php <?php function foo() { echo "checkme::foo\n"; } class Bar { function foo() { echo "checkme::bar::foo\n"; } } ?> results in checkme::foo checkme::bar::foo for example, when my assumption of how the function works should have the code results in undefined function and class errors.Tested latest CVS on a Win32 machine, same problem. Here's a very simple test: randominclude.php <?php function foobar() { echo "HI"; } ?> checksyntax.php <?php if (php_check_syntax('randominclude.php')) { echo "passed"; foobar(); } ?> Calling checksyntax.php via Module/CLI/CGI results in: passedHI As opposed to: passed Fatal error: Call to undefined function foobar() in ...My vote is for this function, and for updating the docs. Also the comments in the php.net manual should be updated, as this isn't really a bug. Simply saying that the function is being misused, or saying this is bug is not a good thing. People just need to understand its usage. The simplest way I can see of updating the doc, is including a more useful example. Perhaps mentioning possible usages (checking the syntax of a file for it's first load into a caching engine for example). <?php $error_message = ""; $filename = "./tests.php"; //Check out $filename if(!php_check_syntax($filename, $error_message)) { //Display an error message, the code is bad printf("Errors were found in the file %s:\n\n%s\n", $filename, $error_message); } else { //Execute the valid code include_once ($filename); } ?>The person above posted this: ---------------------- <?php $error_message = ""; $filename = "./tests.php"; //Check out $filename if(!php_check_syntax($filename, $error_message)) { //Display an error message, the code is bad printf("Errors were found in the file %s:\n\n%s\n", $filename, $error_message); } else { //Execute the valid code include_once ($filename); } ------------------------- To use in the docs. The problem is this code is INCORRECT. The function php_check_syntax( ) INCLUDES THE FILE in php 5.0.2 This means you get redeclare errors if you try to check the syntax and then include the file. The example code SHOULD be: ----------------- <?php $filename = '/path/to/somefile.php'; $err = (string) null; if( !php_check_syntax( $filename, $err ) ) { print "Syntax Error!" die( ); } // proceed, because at this point the file is included // or script is dead. -----------------------------There is one other difference between include and php_check_syntax that should be noted in the manual. Aside from supressing output buffer, it only includes functions and classes, it does not set or affect global variables, the way include() would. If you have "test.php" $myvar = 1; echo $myvar; function myfunction() {} class myclass {} include ("test.php") will set $myvar, print $myvar and set myfunction & myclass php_check_syntax("test.php") will ONLY include myfunction & myclassI would have to agree with most of the other posters, that the php_check_syntax() function as it stands right now does MORE than its name implies. I feel that a true php_check_syntax() function that STRICTLY checks the syntax of a file or string and returns TRUE/FALSE and has reference to an error message is the best. In fact, I would also like to see the possibility of having an additional reference variable for the line number of an error. I am developing a fairly advanced framework that does compile certain aspects of a web site dynamically. At first glance, I tried to implement this function to check the syntax of the file both before saving it and even before including it in the future. However, I ran into some confusing messages about my classes being redefined and I couldn't understand until I read the docs closer. The simple fact is that the REAL functionality of this function is the syntax checking, NOT the including. Any PHP programmer with more than 5 minutes of experience can probably include an external file. So adding that particular aspect of the functionality to php_check_syntax() seems useless. The code I have in my system goes a little like this: <?php ... function includeafile($sFile, $bOnce = FALSE) { if (file_exists($sFile)) { if (!@php_check_syntax($sFile, $sMessage)) { error_handler("File '$sFile' has a syntax error: $sMessage", __LINE__, __FILE__); } else { ... // go on including the file, etc. } } return FALSE; } ?> Now, I started getting errors all of a sudden saying that a class in the file being included is being redeclared. I thought that odd since I set $bOnce = TRUE, so it shouldn't ever be included more than once. I think ideally I should be able to do this: <?php ... function includeafile($sFile, $bOnce = FALSE) { if (file_exists($sFile)) { if (!@php_check_syntax($sFile, $sMessage, $iLine)) { error_handler("File '$sFile' has a syntax error: $sMessage", $iLine, $sFile); } else { ... // go on including the file, etc. // it should not raise redeclaring errors } } return FALSE; } ?> Basically, it should: 1. Not include the file at all, just strictly do a lint check. 2. It would be nice to be able to get the line number of the file (for debugging purposes). At the lowest level, this function should be able to run like described by many others: <?php if (php_check_syntax($file, $message)) { include $file; } else { error("Syntax Error: '$message'"); } ?> It should be up to the PHP programmer to include the file...This was fixed by removing the function altogether. It was nice idea but not one that could ever work reliably/be stable. Use something like 'system("php -l foo.php");' to check the syntax..