如何使用symfony从一个类的方法访问另一个类中的变量值


how to access variable value from method of one class from another class using symfony

我是symfony框架的新手。我有一个来自类的一个方法的变量值。我需要从另一个类方法访问该值。有人能帮我用symfony吗。提前感谢

class A():
   method a():
      s = 10;
class B():
   method b():
      ----

我需要使用symfony从方法b访问s的值。

这不是任何类型的特定于Symfony的(不是特定于事件php的,这只是OOP的基础)。

回答你的问题(以非常一般的方式)。

一个可能的解决方案是uou可以使$s变量成为A:类的公共字段

class A
{
    public $s;
    public function a()
    {
        $this->s = 10;
    }
}
class B
{
    public function b()
    {
        $a = new A();
        $a->a(); //you need this to set value, maybe this should be in constructor?
        $s = $a->s; // this will give you your $s value (10)
    }
}