开发者

PHP Require Global Scope

开发者 https://www.devze.com 2023-01-12 15:35 出处:网络
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 glo

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?


$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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消