简体   繁体   中英

How access outside variables from within class?

I have two class called them class A and B. I created the 'A' class. And in this i create a 'B' class. How can i access the 'A' class variable from 'B' class?

class A
{
    var letter;
    var writers;

    function __construct()
    {
        $this-letter = 'SOMETHING';
        $this->writers = new B;         
    }

}

class B extends Writers
{
    function __construct()
    {
        parent::__construct();
        echo $letter; //This is where i want to acces outside variable (CLASS 'A')
    }

}

I hope i was clear. I'm just rookie on OOP-ing. Please help me.

You can't, because $letter in A is was not declared public and B doesn't extend A.

If you don't want to expose A's data (one of the important OOP principles), you should use encapsulation. Create a getter in the A class

public function getLetter()
{
    return $this->letter;
}

And then in B's construct method, create an instance of A and use said getter

$a = new A();
$letter = $a->getLetter();

In case you really need to use this structure, I guess you could do something like this:

class A
{
    var letter;
    var writers;

    function __construct()
    {
        $this-letter = 'SOMETHING';
        $this->writers = new B($this);         
    }

}

class B extends Writers
{
    var $a;

    function __construct(A $a)
    {
        parent::__construct();
        $this->a = $a;
        echo $this->a->letter; 
    }

}

This way, B would hold a reference to the A object it was created by. However, I rather recommend you to change your class topology, if possible.

Your B class needs to somehow have a reference to an A object. You could simply add to B :

class B extends Writers
{
    private $a;

    function __construct()
    {
        parent::__construct();
        echo $letter; //This is where i want to acces outside variable (CLASS 'A')
    }

   public function A($a)
   {
        $this->a = $a;
   }
}

Next, simply have your A object give a reference to itself to its B object.

class A
{
    var letter;
    var writers;

    function __construct()
    {
        $this-letter = 'SOMETHING';
        $this->writers = new B;    
        $this->writers->A($this);
    }

}

The alternative would be static method.

It's hard to tell exactly what you want, but as I interpreted it:

class A extends Writers
{
    var letter;
    var writers;

    function __construct()
    {
        $this-letter = 'SOMETHING';
        $this->writers = new B;         
    }

}

class B extends A
{
    function __construct()
    {
        parent::__construct();
        echo $this->letter; //This is where i want to acces outside variable (CLASS 'A')
    }

}

You can use static variable. like this:

class A
{
    static $letter;
    function __construct()
    {
        self::$letter = 'SOMETHING';
    }

}
$objA = new A();

class B
{
    function __construct()
    {
        $letter = A::$letter;
        echo $letter; //print SOMETHING
    }

}

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