-1
class Member
{
    private $_member_id;
    private $_first_name;
    private $_last_name;

    public function __construct($member_id, $first_name, $last_name)
    {
        $this->_member_id  = $member_id;
        $this->_first_name = $first_name;
        $this->_last_name  = $last_name;

    }

    public function changeFirstName($new_first_name)
    {
        $this->_first_name = $new_first_name;

    }

    public function changeLastName($new_last_name)
    {
        $this->_last_name = $new_last_name;
    }

    public function returnData()
    {

        return $this->_first_name;
        return $this->_last_name;

    }

}

$person1 = new Member(43, "Tom", "Jones");
$person1->changeFirstName("James");

echo $person1->returnData();

Quick question, I'm learning PHP Classes, I'm trying to return _first_name and _last_name separately from this class.

So echoing echo $person1->returnData(); will return the first name, how do I return the second name too separately? echo $person1->returnData("_last_name"); something like that? But I know its wrong!

tereško
  • 58,060
  • 25
  • 98
  • 150
yetteh
  • 13
  • 2

7 Answers7

2

Popular approach is using getters in class:

public function getFirstName()
{
    return $this->_first_name;
}

public function getLastName()
{
    return $this->_last_name;
}

Example:

echo $person1->getLastName() . ' ' . $person1->getFirstName();

If you want to return several values at a time, then use array:

public function returnData()
{
    return [         
        'first_name' => $this->_first_name,
        'last_name' => $this->_last_name,
    ];
}

Example:

$personData = $person1->returnData();
echo $personData['first_name'] . ' ' . $personData['last_name'];
u_mulder
  • 54,101
  • 5
  • 48
  • 64
1

You can return value for specific variable using this way.

public function returnData($name)
{

    return $this->$name;

}

To get value of any variable, you can get it by $person1->returnData("_last_name");

For first name,

echo $person1->returnData("_first_name");

For second name,

echo $person1->returnData("_last_name");
Ghanshyam Katriya
  • 1,071
  • 1
  • 11
  • 37
0

You can try this:

public function returnData()
{

    return array($this->_first_name, $this->_last_name);

}

// ...

list($firstName, $lastName) = $person1->returnData();
echo $firstName . ' ' . $lastName;
T. AKROUT
  • 1,719
  • 8
  • 18
0

You can't return 2 values in same functions like this.

You must return an array or object:

public function returnData(){
    return [
        'first_name' => $this->_first_name,
        'last_name' => $this->_last_name
    ];
}

$first_name = $person1->returnData()['first_name'];
Alex197
  • 853
  • 2
  • 10
  • 25
0

You have two returns in the returnData() function:

public function returnData()
    {

        return $this->_first_name;
        return $this->_last_name;

    }

The second one will not get executed, because the function exits after the frist. Please have a look at getters and setters - you need separate functions for the separate properties - or return them as an Array.

lehel
  • 431
  • 4
  • 10
0

You can't directly do as you want. Cause the first return will exit from the function. However there are multiple ways to work around that limitation, infact such question was already answered here:

Multiple returns from function

I'm sure it will help you.

Vladmyr
  • 9
  • 2
0

Your function returnData() stops after the first return it encounters. You should create two separate functions getFirstName and getLastName:

public function getFirstName() {
    return $this->_first_name;
}
public function getLastName() {
    return $this->_last_name;
}

You can also create one function with parameter and if statements to return the value needed, but i do not recommend it since it would be an unecessary test.

Rirrsmo
  • 11
  • 3