简体   繁体   English

如何从类中访问外部变量?

[英]How access outside variables from within class?

I have two class called them class A and B. I created the 'A' class. 我有两个类叫做A类和B类。我创建了'A'类。 And in this i create a 'B' class. 在这个我创建一个'B'类。 How can i access the 'A' class variable from 'B' class? 如何从'B'类访问'A'类变量?

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. 我只是OOP的新秀。 Please help me. 请帮我。

You can't, because $letter in A is was not declared public and B doesn't extend A. 你不能,因为A中的$letter未被公开 ,B不会扩展A.

If you don't want to expose A's data (one of the important OOP principles), you should use encapsulation. 如果您不想公开A的数据(重要的OOP原则之一),则应使用封装。 Create a getter in the A class 在A类中创建一个getter

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

And then in B's construct method, create an instance of A and use said getter 然后在B的构造方法中,创建一个A的实例并使用所述的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. 这样, B将保存对其创建的A对象的引用。 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. 你的B类需要以某种方式引用A对象。 You could simply add to B : 你可以简单地添加到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. 接下来,让您的A对象为其B对象提供对自身的引用。

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
    }

}

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

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