0

Hi please have a look on bellow code.

<?php
class A
{
    public $name;

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

class B extends A
{
    public function setName()
    {
        $this->name = 'Prasad';
    }
}

$obj = new B();

echo $obj->getName();

?>

Here It's display nothing when I echo the name. Related to the $name in class A. Is this issue is with getName or setName? How can I set the $name variable in class A from extended class B. And how can I get that from a class B object. Appreciate any hint or explanation on what I have missed.

Prasad Rajapaksha
  • 6,118
  • 10
  • 36
  • 52

5 Answers5

1

You didn't set the name (using $obj->setName()) before.

Niloct
  • 9,491
  • 3
  • 44
  • 57
1

You are most of the way there with your code, in fact you would have a working example if you added a line telling the code to call the setName() function:

$obj = new B();
$obj->setName();
echo $obj->getName();

More typically you would use a set function with a parameter, then pass the value you want to set. You would also set the $name property to protected, which means the value must be accessed via the set & get methods (more on visibility in the manual):

<?php
class A
{
    protected $name;

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

class B extends A
{
    public function setName($name)
    {
        $this->name = $name;
    }
}

$obj = new B();
$obj->setName('Prasad');
echo $obj->getName();

?>
John C
  • 8,223
  • 2
  • 36
  • 47
1

Technically, it's echoing the $name variable (which is undefined at that point). Unfortunately, it hasn't been set yet. Try using $obj->setName() to set the name.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
1

Yes, as SomeKittens suggested you need to call setName() first.

class A
{
    public $name;

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

class B extends A
{
    public function setName()
    {
        $this->name = 'Prasad';
    }
}

$obj = new B();
$obj->setName();
echo $obj->getName();

However, it might be better to perform the setting of the name in the constructor of B, as:

class A
{
    public $name;

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

class B extends A
{
    public function B()
    {
        $this->name = 'Prasad';
    }
}

$obj = new B();

echo $obj->getName();

This printed Prasad for me using http://writecodeonline.com/php/ to test the code.

Even better, pass the name 'Prasad' when creating the new B object, as:

class A
{
    public $name;

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

class B extends A
{
    public function B( $value = 'Prasad' )
    { 
        $this->name = $value;
    }
}

$obj = new B();

echo $obj->getName(), "<br>";

$obj = new B( 'John' );

echo $obj->getName();
HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41
0

It's because you aren't setting the name attribute first. Call B->setName() and then you can get the name by calling B->getName().

James Woodruff
  • 963
  • 6
  • 10