tags:

views:

65

answers:

5

Hey.

Please consider the code below.

class A {
  public function __construct() {

  }
}

class B extends A {
  public $a = "a";
  public $b = "b";
  public $c = "c";
}

How do I get the class B's public variables from within the parent class without knowing precisely what they are?

A: 

If class B extends class A, I don't suppose it makes any OO sense for class A to be able to access the public variables of its child.

Edit I would be grateful if the people who downvoted me would reason their decision, thank you.

zahstra
OK: you are downvoted as a B instance running a method declared in A can access any B-declared variable or function. It is a matter of interpretation of the question though, coming back I realize it multiple meanings could be derived from it. A 'pure A' object could of course not access it, but then there is no B object to talk about as indicated in the question. As getting properties is as simple as `get_object_vars()`, we assume inspecting within a scope which has access to private/protected variables.
Wrikken
Thank you for the explanation. I replied in the spirit of Felix Kling's comment. ClassA, being a parent class, cannot possibly know about the public variables of the classes that inherit from it.
zahstra
A "pure A" object could definitely access it. They are declared `public`, not `protected`...
ircmaxell
@ircmaxell: and that's why my interpretation of the question was different: if 'pure A', a `get_object_vars($b);` would suffice, so I still assume a method declared in A but run in a B instance which has access to protected/privare variables, but does not want to ( or at least filter those out).
Wrikken
+2  A: 

In A create an instance of B and then use

http://www.php.net/manual/en/reflectionclass.getproperties.php

Anyways, what are you trying to do ?

Alex
A: 

Inject the the B-Class-Object into the A-Class-Object:

class A {
    public $b;

    public function __construct(B $b_object) {
        $this->b = $b;
    }
}

class B {
  public $a = "a";
  public $b = "b";
  public $c = "c";
}

So the constructor waits for a Objekt of B here. And now let's use it.

$b = new B();
$a = new A($b);

$a->b->a;
$a->b->b;
$a->b->c

EDIT: Sorry, I didn't see that class B extends class A. So just forget about my solution :)

faileN
+8  A: 
class A {
   public $d;
   public function __construct() {
      $reflect = new ReflectionClass($this);
      $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
      var_dump($props);
   }
}

class B extends A {
  public $a = "a";
  private $b = "b";
  public $c = "c";
}
new B();

Output (notice no 'b', but A's public 'd' is in there, with a mention it's declared in A):

array(3) {
  [0]=>
  &object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(1) "a"
    ["class"]=>
    string(1) "B"
  }
  [1]=>
  &object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(1) "c"
    ["class"]=>
    string(1) "B"
  }
  [2]=>
  &object(ReflectionProperty)#5 (2) {
    ["name"]=>
    string(1) "d"
    ["class"]=>
    string(1) "A"
  }
}
Wrikken
A: 

A subclass inherits from the superclass. Just as a father doesn't inherit any genes from his daughter, only gives them, this isn't possible. Well, technically it is, but it isn't the correct way of implementing object-oriented behavior.

I recommend giving the variable to the superclass, that way it is accessible by both classes, although I'm unsure what you're using it for.

Hope that helps!

Allen Gingrich