0

I have the following 3 classes :

class ParentClass {
    protected $myVar = array();

    function __construct() {
        var_dump($this->myVar);
    }
}

class FirstClass extends ParentClass {
    protected $myVar = array('defaultVal');
}

class SecondClass extends FirstClass {
    protected $myVar = array('anotherVal');
}

If I was to do the following:

$class = new SecondClass();

Then I would get array('anotherVal') what can I add to the construct of ParentClass so that I would actually get array('defaultVal', 'anotherVal')

Thanks in advance!

4
  • 2
    Obvious answer is: function __construct() { $this->myVar = array('defaultVal', 'anotherVal'); } Otherwise, you need to be more specific about what you're trying to accomplish (and possibly why). Commented Jul 13, 2011 at 22:57
  • 3
    I do not think that this question is all that difficult to understand. He has a class hierarchical structure that he wants to be able to alter a variable in certain ways to be able to produce different behaviors in the system. So by altering the parent to 2 static values (the ones he specified) you are not answering the question in any way. Your just being a smart ass. Commented Jul 13, 2011 at 23:06
  • 2
    Thanks for the name calling, Michael ;) Try to keep in mind that this is the comment section, not the answer section. You can add comments on the questions here, even slightly snarky ones. Answers should go below. However, my comment is vindicated by the mere fact that, as of this writing, none of the below answers speak to his question which is: what can I add to the construct of ParentClass so that I would actually get... Commented Jul 13, 2011 at 23:12
  • really? a downvote? what for? Commented Jul 13, 2011 at 23:24

4 Answers 4

5

For some odd reason, stack overflow keeps erasing my previous answer. So here is the new one.

class ParentClass {
    protected $myVar;

    function __construct() {
        $this->myVar = array();
    }
}

class FirstClass extends ParentClass {
    function __construct() {
        parent::__construct();
        $this->myVar[] = 'default val';
    }
}

class SecondClass extends FirstClass {
    function __construct() {
        parent::__construct();
        $this->myVar[] = 'another val';
    }

    public function printArr() {
        print_r($this->myVar);
    }
}

$class = new Secondclass();
$class->printArr();
Sign up to request clarification or add additional context in comments.

2 Comments

Oooops!! i did not read "What can i add to parent class to get" The whole parent class thing... my bad. Let me go figure that out
Deferring to your answer since we reached the same solution, but your timestamp is older :)
0

Change your assignments to array_push().

Comments

0

Using ideas from this page, I would suggest the following:

class ParentClass {
    protected $myVar = array();

    function __construct() {
        var_dump($this->myVar);
    }
}

class FirstClass extends ParentClass {
    function __construct() {
        array_unshift($this->myVar, "defaultVar");
        parent::__construct();
    }
}

class SecondClass extends FirstClass {
    function __construct() {
        array_unshift($this->myVar, "anotherVal");
        parent::__construct();
    }
}
$class = new ParentClass();
$class1 = new FirstClass();
$class2 = new SecondClass();

Which gives me the following output:

array(0) {
}
array(1) {
  [0]=>
  string(10) "defaultVar"
}
array(2) {
  [0]=>
  string(10) "defaultVar"
  [1]=>
  string(10) "anotherVal"
}

Comments

-1

After furthur research i found out an easy way to do it get_class_vars:

class ParentClass {
    protected $myVar = array();

    function __construct() {
        $firstClassVars = get_class_vars('FirstClass');
        $this->myVar= array_merge($this->myVar, $firstClassVars['myVar']);
        var_dump($this->myVar);
    }
}

This solves my problems...

Thanks anyway guys!

4 Comments

While this may accomplish what you're trying to do, it's really not good practice to force your parent class to know about its children like this. Check out @Michael's solution above for a cleaner way to do this.
@Lizard Pretend that your ParentClass has 20 sub classes and each one of those has 1 to 2 sub classes themselves. Imagine the size of your __construct(). The if statements will be insane and the rigidity of that code is going to be such problems. Though your code works, it works on small solutions and is almost non-scalable.
But it doesn't... my question is about 20 sub classes, I asked about 2 sub classes, and absolutely no if statement is used. Please answer the questions rather than what you think the problem might be
@Lizard The problem is not that i did not understand the problem, the problem is i saw a serious design flaw and tried to warn you of it. Please try to be a "know it all" and do better programming practices. These kinds of programming practices has/will cause so much problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.