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.

1
  • 3
    Have you tried calling setName() first? Commented Jul 16, 2012 at 1:52

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great... Thanks for pinting that out.
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.

Comments

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();

?>

Comments

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();

Comments

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().

1 Comment

setName() without params because the name are static, in this case.

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.