user2432700
user2432700

Reputation:

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.

Upvotes: 0

Views: 90

Answers (5)

mreza
mreza

Reputation: 41

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
    }

}

Upvotes: -1

Näbil Y
Näbil Y

Reputation: 1650

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')
    }

}

Upvotes: 0

federicot
federicot

Reputation: 12341

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

Upvotes: 1

TheWolf
TheWolf

Reputation: 1385

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.

Upvotes: 1

SolarBear
SolarBear

Reputation: 4619

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.

Upvotes: 0

Related Questions