php - Access variable from another class that was passed in constructor

I'm coding a WP plugin using PHP OOP. I'm passing some classes to my main class, so that I will be able to acc

I'm coding a WP plugin using PHP OOP. I'm passing some classes to my main class, so that I will be able to access variables and methods from my main class to other classes. In fact I'm able to access methods, but not variables. For example we have Main and Secondary classes:

class Main {
    public $secondary;

    public function __construct($secondary) {
        $this->secondary= $secondary;
    }

    public function methodFromSecondary() {
        echo $this->secondary->secondaryMethod();
    }

    public function variableFromSecondary() {
        echo $this->secondary->secondaryVariable;
    }
}

class Secondary {
   public $secondaryVariable;

   public function __construct() {
       $this->secondaryVariable = 'some variable string';
   }

   public function secondaryMethod() {
       return 'some mothod string';
   }
}

$secondary = new Secondary();
$main = new Main($secondary);

If I call $main->methodFromSecondary() it outputs 'some mothod string' and if I call $main->variableFromSecondary(), WP outputs that site is experiencing technical issues. So how can I access a variable to output 'some variable string' as I access method?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745299401a4621343.html

相关推荐

  • php - Access variable from another class that was passed in constructor

    I'm coding a WP plugin using PHP OOP. I'm passing some classes to my main class, so that I will be able to acc

    8小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信