tags:

views:

36

answers:

2

Hello,

I am having an issue in php... I don't fully understand how the whole require() thing works. My understanding is that it takes the current class's member variables and makes them global inside the required file. If this is the case, then why does it not also take the base class's member variables and make them global?

baseclass.php:

class BaseClass {
 var $user;
}

myclass.php:

class MyClass extends BaseClass {
 function doSomething() {
  require "page.php"
 }
} 

page.php:

$this->user // <- this is out of scope?
+2  A: 

$this->user is available. I just tested it.

require() works as if the required file were in the require command's place. Nothing more, nothing less.

In your example, the required file gets the function's scope.

var $varname is old style: In PHP 5, better use one of

public $varname
private $varname
protected $varname

to declare variables.

Pekka
On second thought, I'm removing my "not perfect style" remark. Although one could argue that if your methods are so big you need to put them into external files, you have a design problem... But it's not pertinent to the question.
Pekka
very interesting, I can do a print_r on the object(with success) outside of the require but if I do it inside I get nothing.
DaveC
@DaveC a `print_r($this);` works fine for me from within the include. Can you show the exact code of your included file?
Pekka
Your assistance helped me deduce a design flaw, thank you for your assistance.
DaveC
A: 

I will assume that myclass.php has include of baseclass.php.

when you have include or require(difference is only the type of error they return), php includes and evaluates the specified file(page.php), with scopes of the method you including file from(doSomething();). Therefore I do not see why you would have out of scope issue.

aromawebdesign.com