简体   繁体   中英

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. Is there a better way ?

Thanks!

EDIT

okay, to clarify another.php is literally another file. 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.

Answered: My bad... I included another.php from index.php.. I see scoping still applies.. thanks everyone..

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 . That means that the include sees everything in scope in that method. Including $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 .


Example, using the PHP interactive shell:

[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. If you need to access $var , either make it public, or return it from a member function. You should read more about visibility from the PHP Manual

Edit: What makes your situation interesting is that you are calling include from a member function. include will inherit the scope from which it is called. So, technically, you can call $this->var from another.php . However, I strongly encourage against this practice . If another.php gets included anywhere else, you will get errors. Please, please don't do this. It's terrible programming practice.

If you really must, add these lines to A.php :

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

And then change another.php to this:

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). This is such a terrible way to go about things in PHP.

But doing things a better way, you could make the variable 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):

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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