php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #11047 Cannot put require() or include() within a class definition
Submitted: 2001-05-23 05:36 UTC Modified: 2001-05-24 15:02 UTC
From: miguel222 at mindless dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.4pl1 OS: RH Linux 7.0
Private report: No CVE-ID: None
 [2001-05-23 05:36 UTC] miguel222 at mindless dot com
class foo {
  function foo() {
  }

  require "foo_functions.php";
}

~~~~

The above code returns the following parse error:

Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in foo.class.php on line 5

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-05-23 09:44 UTC] cynic@php.net
Well, the truth is that there's no real preprocessor in PHP. The fact that it's impossible to use include*() or require*() in class definitions is by design, and is very unlikely (read "guaranteed not to") to change during the life of 4.x.
 [2001-05-23 13:19 UTC] miguel222 at mindless dot com
how odd.. I didn't see your "there is no real preprocessor" note earlier.

Alright. in that case, perhaps the documentation for require should be changed to say that require *is* subject to containing control structures.
 [2001-05-23 14:39 UTC] cynic@php.net
That's how it is, and how it'll be in foreseeable future.
 [2001-05-23 15:13 UTC] miguel222 at mindless dot com
This seems like an easy enough addition, and it makes building large libraries of homologous classes MUCH easier.  Of course, there is a work-around: I can inherit numerous intermediate classes to simulate multiple inheritance, but that could theoretically become a VERY large overhead, if you have many "base" classes to inherit.  Doing so also makes it difficult to read the resulting code.
 [2001-05-23 15:19 UTC] miguel222 at mindless dot com
Besides... I thought require was supposed to act as a preprocessor pass. From the PHP docs:

~~~

The require() statement replaces itself with the specified file, much like the C preprocessor's #include works. 

[snip]

For instance, require() is not subject to any containing control structures. 

~~~


 [2001-05-24 15:02 UTC] cynic@php.net
Well, 

> The require() statement replaces itself with the specified
> file, much like the C preprocessor's #include works. 

is an exact description. It doesn't that require() is part of PHP's preprocessor (there's none), but works _much like_ C preprocessor's #include.

And it really is NOT subject to containing control structures. As I understand it (and I'm no expert, PHP (Zend) works similarly to C _sans_ preprocessor: the code is compiled and then executed, with some of the C preprocessor features "hacked into" the compilation.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 13:01:29 2024 UTC