Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

require/include file within class

Status
Not open for further replies.

conrai

Programmer
Nov 21, 2004
10
FI
Hi, I know this is probably very simple, but I just don't get it running.

I want to include a file within a class, so that it is globally visible within the whole class.

Within the functions of a class this works:
require ("language/" . $this->language . ".php");

Yet it doesn't outside of the functions. That's how I imagine it:

class Page {
var $language = 'en';
require ("language/$this->language.php");

function someFunction() {...}
}

Here I get the error:
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR'...

How can I include a file for the whole class, so that it is globally visible? Thanks for your help!

c.
 
What is in the file?
Let's see an example.
 
You can't include() or require() a file in a class definition like this. According to the documentation (Chapter 13: Classes and Objects), you can't break a class definition into multiple files or multiple PHP blocks. The include() and require() statements imply a break in the PHP block, as evidenced by the fact that the contents of the included file has to be enclosed in PHP tags to be treated as code.

However, the quick and highly irresponsible test that I just ran (using PHP 4.3.9) indicates that you can include files within the functions contained in a class, including the constructor. Consider the following code:
Code:
File: test.php
<?php
class A {
	var $f;
	function A() {
		include ("t1.php");
	}
	function b() {
		echo $this->va;
	}
}
$q=new A;
echo $q->va;
$q->b();
?>

File: t1.php
<?php $this->va = 7; ?>
When I run test.php, I get the output "77", just as you'd expect.

Of course, this is just an example. I personally don't think this is a particularly good approach. It's not entirely clear what you're trying to do, but it would probably be better to try to design the program so that the file can be require()ed outside the class definition.
 
Not sure , but maybe you have to use:

Code:
require ("language/{$this->language}.php");

// or 
require ("language/".$this->language.".php");

If you can, tell as the line (number) where it gives the error.


Regards,
PM

___
____
 
Thanks for your immediate replies! And sorry for the late response.

@AdaHacker: your solution with include()ing the file in the constructor worked. I tried this before already, yet I had defined the variables in my file to be inluded without this->.

thanks again, i really appreciate this forum, somtimes it's just better to ask someone else instead of trying for hours and hours ;)

br,
c.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top