0

I tried many research but I can't find this one. The only problem I am having is that: the $name is empty inside class Daddy.

I tried researching: how to access public variables in extended class but it seems that they haven't done anything like this. I just don't want to re-declare the variables in Grandpa Class. I want it accessible inside Daddy Class.

I have this parent class

<?php
    class GrandPa
    {
        public $name;

        public function name($name)
        {
            $this->name = $name;
            return $this;
        }

        public function get()
        {
            $daddy = new Daddy;
            return $daddy->displayGrandPaName();
        }
    }
?>

Then the extended one:

<?php
    class Daddy extends GrandPa // Inherited class
    {
        public function displayGrandPaName()
        {
            var_dump($this->name.'ss');
            return $this->name; 
        }

    }
?>

When I initiate:

<?php
    $g = new GrandPa;
    echo $g->name('aw')->get();
?>

It var dumps:

string(2) "ss"

The expected result would be:

awss
KD.S.T.
  • 573
  • 1
  • 5
  • 27
  • It doesn't answer your question, but your design is wanting a bit here. Your base class should not know about any subclass that it might have. Are you sure you've nailed your approach here? Also there are industry-wide conventions around the naming of accessor/mutator methods. You probably want those to be `getName` and `setName`. Although perhaps this is just purely example code, and not extracted rom your actual codebase. – Adam Cameron Jun 19 '18 at 06:30
  • @AdamCameron it is an example. the problem is `the $name is empty inside class Daddy.` – KD.S.T. Jun 19 '18 at 06:35

1 Answers1

1

See my comment on the question for some design considerations.

Your issue is that whilst your setting the name property of your $g object, but your get method creates an entirely new (and, accordingly, different) object, and gets the name from that. As you haven't set the name on that object, it is blank. Which is what you're seeing.

It's difficult to best answer your question as I think your design is just wrong, but to get the value of the property from your object, your get method ought to be

public function get()
{
    return $this->name;
}

A more accurate example of what you seem to be wanting to do would be this:

class GrandPa
{
    public $name;

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    public function getName()
    {
        return $this->name;
    }
}

class Daddy extends GrandPa
{
    public function displayName()
    {
        var_dump($this->name.'ss');
        return $this->name; 
    }

}

$d = new Daddy();
echo $d->setName('aw')->displayName();
Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • Can you change Daddy to GrandPa? – KD.S.T. Jun 19 '18 at 06:44
  • So you can't call a `child class` inside `parent class`? since parent class has not yet finished setting the variables then I call child class which makes parent class variables empty. – KD.S.T. Jun 19 '18 at 06:48
  • Well that's not it, no. From a design perspective it's an anti-pattern. Here's some reading: https://softwareengineering.stackexchange.com/questions/219543/should-a-class-know-about-its-subclasses. Secondly and that aside, you're not understanding the issue. It's nothing about "parent class has not yet finished setting the variables", it's purely because you're setting the property on one object, and accessing it from a completely different object. I suspect you need to read up on the difference between a class and an object. [...] – Adam Cameron Jun 19 '18 at 06:55
  • (couldn't fit the URL in to the previous comment. Reading here: https://stackoverflow.com/questions/13775226/what-is-the-difference-between-classes-and-object-instances) – Adam Cameron Jun 19 '18 at 06:55
  • I see. I should Make a Class for only declaring variables. Then extends to child classes so that any class can access the variables. – KD.S.T. Jun 19 '18 at 07:15
  • Not necessarily / not really. I don't at all mean to sound dismissive, but this is not the venue for a protracted discussion about OOP. There's plenty of reading material out there. I recommend you do some research before continuing too much further. – Adam Cameron Jun 19 '18 at 07:17
  • I am re-constructing my classes. So far base on your answer I now have a lead on which way to go. Ill mark this as an answer. Thanks! – KD.S.T. Jun 19 '18 at 07:18
  • OK, feel free to put your resultant code up on https://codereview.stackexchange.com/ – Adam Cameron Jun 19 '18 at 07:27