简体   繁体   English

从访问属性包括PHP类方法中

[英]Access property from include inside a class method in PHP

How do you make a class property available to the other included file inside the same class' method? 你如何让一个类的属性可用于同一类的方法里面包含的其他文件?

// file A.php
class A
{
    private $var = 1;

    public function go()
    {
        include('another.php');
    }
}

in another file: 在另一个文件中:

// this is another.php file
// how can I access class A->var?
echo $var; // this can't be right

Is this possible given the scope. 给定范围可能吗? If var is an array then we can use extract but if var is not, we can wrap it in an array. 如果var是一个数组,则可以使用extract,但是如果var不是,则可以将其包装在数组中。 Is there a better way ? 有没有更好的办法

Thanks! 谢谢!

EDIT 编辑

okay, to clarify another.php is literally another file. 还好,澄清another.php简直是另一个文件。 Basically, in the above examples, we have 2 files A.php which contains class A and another.php which is another file/script executing something. 基本上,在上面的示例中,我们有2个文件A.php包含类A,另一个文件another.php是另一个执行某些操作的文件/脚本。

Answered: My bad... I included another.php from index.php.. I see scoping still applies.. thanks everyone.. 回答:我的糟糕...我从index.php包含了另一个.php。我看到范围仍然适用..谢谢大家..

Your question seems to be, " when inside a file included from within a method, how do I access a private instance member? " Right? 您的问题似乎是,“ 在方法中包含的文件中时,如何访问私有实例成员? ”对吗?

In your example code, you're including a file inside a method. 在示例代码中,您将在方法内包含一个文件。

Methods are just functions. 方法只是功能。 Like all other areas of PHP, the file that gets included will inherit the entire current scope . 与PHP的所有其他区域一样,包含的文件将继承整个当前scope That means that the include sees everything in scope in that method. 这意味着,包括看到的范围,该方法的一切。 Including $this . 包括$this

In other words, you'd access the property in the include file just like you'd access it from inside the function itself, as $this->var . 换句话说,在包含文件中,就像你从函数本身内部访问它,因为你会访问属性$this->var


Example, using the PHP interactive shell: 例如,使用交互式PHP壳:

[charles@lobotomy /tmp]$ cat test.php
<?php
echo $this->var, "\n";

[charles@lobotomy /tmp]$ php -a
Interactive shell

php > class Test2 { private $var; public function __construct($x) { $this->var = $x; } public function go() { include './test.php'; } }
php > $t = new Test2('Hello, world!');
php > $t->go();
Hello, world!
php > exit
[charles@lobotomy /tmp]$ php --version
PHP 5.4.4 (cli) (built: Jun 14 2012 18:31:18)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.0rc1, Copyright (c) 2002-2012, by Derick Rethans

You have defined $var as private, which means $var can only be accessed by member functions. 您已将$var定义为private,这意味着$var 只能由成员函数访问。 If you need to access $var , either make it public, or return it from a member function. 如果您需要访问$var ,则将其公开,或从成员函数返回。 You should read more about visibility from the PHP Manual 您应该从PHP手册中阅读有关可见性的更多信息。

Edit: What makes your situation interesting is that you are calling include from a member function. 编辑:使您的情况有趣的是,您正在从成员函数调用include include will inherit the scope from which it is called. include将继承调用它的范围。 So, technically, you can call $this->var from another.php . 因此,从技术上讲,您可以从another.php调用$this->var However, I strongly encourage against this practice . 但是,我强烈反对这种做法 If another.php gets included anywhere else, you will get errors. 如果another.php被包含在其他任何地方,您将得到错误。 Please, please don't do this. 拜托, 不要这样做。 It's terrible programming practice. 这是可怕的编程实践。

If you really must, add these lines to A.php : 如果确实需要,请将这些行添加到A.php

$obj = new A();
$obj->go();    // this will call another.php, which will echo "$this->var"

And then change another.php to this: 然后将another.php更改为此:

echo $this->var;

And it will work; 它会起作用; you will get the right output. 您将获得正确的输出。 Note that if you do not declare an instance of class A this will fail (eg, A::go() , A->go() , etc will all fail). 请注意,如果不声明类A的实例,这将失败(例如, A::go()A->go()等都将失败)。 This is such a terrible way to go about things in PHP. 这是处理PHP事情的一种可怕方法。

But doing things a better way, you could make the variable public: 但是,做一个更好的方法,您可以将变量设为public:

class A {
    public $var = 1;  //note, it is public!
    public function go() {
        include('another.php');
    }
}
$obj = new A();
echo $obj->var; //woot!

Or, keep it private (which is better OOP): 或者,将其设为私有(这是更好的OOP):

class A {
    private $var = 1;  //note, it is private

    //make a public function that returns var:
    public function getVar() {
        return $this->var;
    }

    public function go() {
        include('another.php');
    }
}

$obj = new A();
echo $obj->getVar(); //woot!
 class A
{
    public $var = 1;

    public function go()
    {
        include('another.php');
    }
}

$objA = new A();

$objA->go();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM