php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11835 Include and Require does not work inner a class definition.
Submitted: 2001-07-02 09:14 UTC Modified: 2001-07-02 09:27 UTC
From: developer at libero dot it Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.4pl1 OS: win nt 4
Private report: No CVE-ID: None
 [2001-07-02 09:14 UTC] developer at libero dot it
Require nor Include works inner a class file.

This is the file where I define the class:
===== 1.php =====
<?
class test {
  include("2.php"); //the same with require
}
?>
=====  end  =====

And this is the file where I have content.
===== 2.php =====
<?
var $a=1;
function method1 () {
  $this->a++;
}
?>
=====  end  =====

This does not work.
The error is:
========
Parse error:  parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' 
========

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-02 09:19 UTC] jan@php.net
include is not allowed there, use the constructor instead
class {
	function class ()	{
		include('2.php'); //same with require
		}
	}


 [2001-07-02 09:27 UTC] developer at libero dot it
Well
"include is not allowed there" is not sufficient.

I have 200+ classes that have a common declaration of variables and methods.
Everytime I need  to modify this declarations I have to modify this 200+ files.

If I could use require or include there I had only one file to modify.

----- Original Message ----- 

> ID: 11835
> Old-Status: Open
> Status: Closed
> Bug Type: Scripting Engine problem
> 
> include is not allowed there, use the constructor instead
> class {
> function class () {
> include('2.php'); //same with require
> }
> }
> 
> 
> 
> Previous Comments:
> ---------------------------------------------------------------------------
> 
> [2001-07-02 09:14:57] developer@libero.it
> 
> Require nor Include works inner a class file.
> 
> This is the file where I define the class:
> ===== 1.php =====
> <?
> class test {
>   include("2.php"); //the same with require
> }
> ?>
> =====  end  =====
> 
> And this is the file where I have content.
> ===== 2.php =====
> <?
> var $a=1;
> function method1 () {
>   $this->a++;
> }
> ?>
> =====  end  =====
> 
> This does not work.
> The error is:
> ========
> Parse error:  parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' 
> ========
> 
> ---------------------------------------------------------------------------
> 
 [2002-06-05 11:55 UTC] lopez at sydel dot net
Indeed include or class must work inside a class so as to ease multiple definitions of the same variables or functions.  As in my case, all my classes have a save and a delete function which are identical.  include or require will make it easier for me to maintain my code (as what it should be)
 [2010-07-11 01:01 UTC] tbiegacz at gmail dot com
Did you try to define base class with common variables and functions? For example

class Common {
  protected $strCommonVariable;
  protected function commonFunction() { ... }
}

class Specific extends Commmon {
 ...
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 11:01:31 2024 UTC