1

I'm not sure how to explain it, but having a class B referenced by class A, is it possible for class B to interact with class A?

class A {
    function A($name) {
        $this->name = $name;
    }
    function addB($name) {
        $this->b = new B($name);
    }
}
class B {
    function B($name) {
        $this->name = $name;
        echo $a->name; // should echo $name set on class A
    }
}
$a = new A("x");
$a->addB("y");
fcaserio
  • 726
  • 1
  • 9
  • 18

2 Answers2

1

You would use a getter to return the variable.

class A {
  private $myPrivateVar;
  function __construct() {
    $this->myPrivateVar = 100;
  }
  // Accessor (AKA getter)
  public function getMyPrivateVar() {
    return $this->myPrivateVar;
  }
  // Mutator (AKA setter)
  public function setMyPrivateVar($newVar) {
    $this->myPrivateVar = $newVar;
  }
}

class B {
  function __construct() {
    $a = new A();
    $thePrivateVarFromA = $a->getMyPrivateVar();
    $newVal = $thePrivateVarFromA * 100;
    $a->setMyPrivateVar($newVal);
  }
}

See this answer for a good breakdown.

Community
  • 1
  • 1
Steven Spasbo
  • 647
  • 7
  • 18
  • And how can I access getMyPrivateVar() from within class B? – fcaserio Nov 11 '15 at 18:28
  • ok, I understand your point, but that's not what I'm asking. Considering a scenario where class B is called from within class A, can class B interect with class A? – fcaserio Nov 11 '15 at 18:38
  • Yes, you could create instance variables of class A in class B, then in A create a new instance of B, and set the variables from A. Each of those would be new instances and independent of each other. However, if A contained a static variable, it would be shared across all instances of class A, which could be set from B. Does that make sense? – Steven Spasbo Nov 11 '15 at 18:57
  • ok, that makes sense, but still not what I was trying to achieve. I was wondering if there was a way to interect with the existent class A instance from within class B (that was called from class A existent instance). Create a new instance of class A is not what I'm looking for. – fcaserio Nov 11 '15 at 19:24
0

Coming back to this questions, this is how I figured to deal with the question raised on this post, sending parent class reference to child class: new _child($name, $this):

class _parent {
    function _parent($name) {
        $this->name = "I'm $name";
        $this->childs = array();
    }
    function addToName($name) {
        $this->name .= " + father of " . $name;
    }
    function addChild($name) {
        $this->childs[] = new _child($name, $this);
    }
}
class _child {
    function _child($name, $parent) {
        $this->name = "I'm $name";
        $this->brothers = 0;
        $parent->addToName($name);
        foreach ($parent->childs as $child) {
            $child->hasBrother($name);
        }
    }
    function hasBrother($name) {
        $this->name .= " + older brother of $name";
        $this->brothers = 1;
    }
}
$a = new _parent("A");
$a->addChild("B1");
$a->addChild("B2");
$a->addChild("B3");
echo "<pre>"; print_r($a); echo "</pre>"; 

Any comments are welcome!

fcaserio
  • 726
  • 1
  • 9
  • 18