-1

If I have the following class example:

<?php
class Person
{
    private $prefix;
    private $givenName;
    private $familyName;
    private $suffix;

    public function setPrefix($prefix)
    {
        $this->prefix = $prefix;
    }

    public function getPrefix()
    {
        return $this->prefix;
    }

    public function setGivenName($gn)
    {
        $this->givenName = $gn;
    }

    public function getGivenName()
    {
        return $this->givenName;
    }

    public function setFamilyName($fn)
    {
        $this->familyName = $fn;
    }

    public function getFamilyName() 
    {
        return $this->familyName;
    }

    public function setSuffix($suffix)
    {
        $this->suffix = $suffix;
    }

    public function getSuffix()
    {
        return $suffix;
    }

}

$person = new Person();
$person->setPrefix("Mr.");
$person->setGivenName("John");

echo($person->getPrefix());
echo($person->getGivenName());

?>

I there a way in PHP (5.4 preferably), to combine these return values into one function, this way it models a little bit more like the revealing module pattern in JavaScript?

UPDATE: OK, I am now beginning to learn that within PHP, it is normative to return a single value from a function, but you "can" return an array of multiple values. This is the ultimate answer to my question and what I will dive into some practices with this understanding.

small example -

function fruit () {
return [
 'a' => 'apple', 
 'b' => 'banana'
];
}
echo fruit()['b'];

Also an article I ran across on stackoverflow on the topic... PHP: Is it possible to return multiple values from a function?

Good luck!

Community
  • 1
  • 1
klewis
  • 7,459
  • 15
  • 58
  • 102
  • Why don't you just return an array ?? or what exactly are you expecting ? – Baba Nov 14 '12 at 15:52
  • I'm new to OOP with PHP, but it just looks like the code can be condensed, in the sense of knowing what is being returned and whats not, why scroll down a million lines of code to see what function is being returned, when you can control all of your returned functions from a single constructor function. – klewis Nov 14 '12 at 15:56
  • 1
    Ofcourse you can! Just return the object himself: `return $this;` – Ron van der Heijden Nov 14 '12 at 15:57
  • ok, that makes sense to me. I just need to learn how to properly return this when it comes down to returning selective PHP functions in a class – klewis Nov 14 '12 at 16:05
  • 1
    You could probably use the magic [__set and __get](http://ch1.php.net/__set) for this. – Louis Huppenbauer Nov 14 '12 at 16:06
  • Sounds good - I'll look into that Louis! – klewis Nov 14 '12 at 16:08

2 Answers2

2

You sound like you want the __get() magic method.

class Thing {

private $property;

public function __get($name) {
    if( isset( $this->$name ) {
        return $this->$name;
    } else {
        throw new Exception('Cannot __get() class property: ' . $name);
    }
}

} // -- end class Thing --

$athing = new Thing();
$prop = $athing->property;

In the case that you want all of the values returned at once, as in Marc B's example, I'd simplify the class design for it thusly:

class Thing {

private $properties = array();

public function getAll() {
    return $properties;
}

public function __get($name) {
    if( isset( $this->properties[$name] ) {
        return $this->properties[$name];
    } else {
        throw new Exception('Cannot __get() class property: ' . $name);
    }
}

} // -- end class Thing --

$athing = new Thing();
$prop   = $athing->property;
$props  = $athing-> getAll();
Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • Or just rename the private to public and remove all those useless methods – Ron van der Heijden Nov 14 '12 at 15:59
  • *OR* leave it how it is and think of the case where you want to allow public *read* access, but leave write access restricted. Also, because you can write your own accessors you can greatly extend what $obj->prop really does in the background. – Sammitch Nov 14 '12 at 16:03
  • Thank you Sammitch for the help! This looks very interesting. – klewis Nov 14 '12 at 16:07
1

Perhaps

public function getAll() {
    return(array('prefix' => $this->prefix, 'givenName' => $this->giveName, etc...));
}
Marc B
  • 356,200
  • 43
  • 426
  • 500