Prasad Rajapaksha
Prasad Rajapaksha

Reputation: 6190

PHP Object Oriented Issue On My Code

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.

Upvotes: 0

Views: 71

Answers (5)

HeatfanJohn
HeatfanJohn

Reputation: 7333

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

Upvotes: 1

John C
John C

Reputation: 8415

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

?>

Upvotes: 1

SomeKittens
SomeKittens

Reputation: 39522

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.

Upvotes: 1

James Woodruff
James Woodruff

Reputation: 973

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

Upvotes: 0

Niloct
Niloct

Reputation: 10015

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

Upvotes: 1

Related Questions