PHP 类继承包含文件中的变量

发布于 2024-11-17 07:35:41 字数 599 浏览 6 评论 0原文

我有这样的:

class Class2 extends Class1 {
.....
 function __construct() {
 parent::__construct();
   $var_in_included_file;
}   
}

class Class1 {
function __construct() {
 include_once('my_file.php')
}
.....
}

my_file.php:

$var_in_included_file=10;

问题是我无法接收 $var_in_included_file 的值。有没有办法在不添加许多代码的情况下接收这个值,例如:

$this->var=$var_in_included_file ....? 

因为我有数千个变量。 谢谢。 更抽象的问题是: 在一些文件中,我收到(来自 $_POST)近 500 个变量。 这些变量已以复杂的方式详细阐述。为了简化这个阐述,我需要创建类继承树 - 但在这种情况下,如果不将它们分配给类变量,这些变量将不会在子类中看到 - 但这会产生大量代码。

I have like:

class Class2 extends Class1 {
.....
 function __construct() {
 parent::__construct();
   $var_in_included_file;
}   
}

class Class1 {
function __construct() {
 include_once('my_file.php')
}
.....
}

my_file.php:

$var_in_included_file=10;

The problem is that I cannot receive value of $var_in_included_file. Is there a way to receive this value without add many codice like:

$this->var=$var_in_included_file ....? 

Because I have many thousands of variables.
Thanks.
More abstract the problem is:
in some file I received (from $_POST) near 500 variable.
These variables have be elaborated in complicated way. For simplify this elaborating I need create tree of class inheritans - but in this case these variables will not seen in child classes without assigning them to class variables - but this produses enormous volume of code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香草可樂 2024-11-24 07:35:41

在第一类中,将变量分配给类变量:

class Class1{
   private $someVariable;

 public function __construct(){
    include_once 'my_file.php';
     // variable declared in my_file.php        
    $this->someVariable = $someVariable;        
    }       

}

现在可以通过 $this->someVariable 在子类中访问该变量。

快乐编码,祝你好运

In class one, assign your variables to class variables:

class Class1{
   private $someVariable;

 public function __construct(){
    include_once 'my_file.php';
     // variable declared in my_file.php        
    $this->someVariable = $someVariable;        
    }       

}

Now that variable is accessible in child class, through $this->someVariable.

Happy coding, good-luck

终止放荡 2024-11-24 07:35:41

正如 include() 中所述变量范围,当您在 __construct()方法,您所包含的文件中变量的范围仅限于 __construct() 方法,而不是类。

您的选择是更改包含文件的内容以在变量名称前面包含 $this-> (即 $this->var_in_included_file = 10; code>) 或在 __construct() 方法中添加 $this->var_in_included_file = $var_in_included_file;

As explained in include() and variable scopes, when you include a file in your __construct() method, the scope of the variables in the file you're including is limited to the __construct() method, not the class.

Your options would be to either change the content of the included file to include a $this-> in front of the variable name (i.e. $this->var_in_included_file = 10;) or add a $this->var_in_included_file = $var_in_included_file; in your __construct() method.

怪我入戏太深 2024-11-24 07:35:41
Class1 {

include_once('my_file.php')
.....
}

不可能

Class1 {

include_once('my_file.php')
.....
}

Is not possible

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文