4

In my application i need many getter and setter and my idea was to generate them from an array, for example:

protected $methods = ['name', 'city'];

With this two parameters, i will need to generate the following methods:

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

public function getName($lang = null) {
  return $this->getEntityValue('name', $lang);
}

And for city, the method will be:

public function getCityAttribute() {
  return $this->getCity();
}

public function getCity($lang = null) {
  return $this->getEntityValue('city', $lang);
}

Sure, i should need to generate the setter too (with the same logic).

As you can see, i will need a method with get<variable_name>Attribute and inside this call get<variable_name> and the other (getName) return even the same method (for each getter) and just change the 'name' parameter.

Every method have the same logic and i would like to generate them "dynamically". I don't know if this is possible..

6
  • Give us an example on what you would expect to see when you run the functions Commented Sep 22, 2016 at 7:32
  • The two "getter" method you see is what i should need to generate. For the city variable, i should need to generate the two exact same method. I update with the city one Commented Sep 22, 2016 at 7:34
  • 1
    Out of curiosity, what are the *Attribute() methods good for? I mean all they do is calling the second method. Commented Sep 22, 2016 at 7:40
  • I'm using Laravel and this is a "special" method. But the questions is about PHP, the name convention is just a Laravel feature :) Commented Sep 22, 2016 at 7:41
  • 3
    You can utilize __call(). Commented Sep 22, 2016 at 7:44

3 Answers 3

2

You can leverage __call() to do this. I'm not going to provide a full implementation but you basically want to do something like:

public function __call($name, $args) {
    // Match the name from the format "get<name>Attribute" and extract <name>.
    // Assert that <name> is in the $methods array.
    // Use <name> to call a function like $this->{'get' . $name}().

    // 2nd Alternative:

    // Match the name from the format "get<name>" and extract <name>.
    // Assert that <name> is in the $methods array.
    // Use <name> to call a function like $this->getEntityValue($name, $args[0]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Send this params(name, city or other) as parameter to universal method(if you don't know what params you can get)

public function getAttribute($value) {
  return $this->get($value);
}

public function get($value, $lang = null) {
  return $this->getEntityValue($value, $lang);
}

If you know yours parameters, you can use this:

public function getNameAttribute() {
    return $this->getName();
}
$value = 'Name'; //for example
$methodName = 'get' . $value . 'Attribute';
$this->$methodName; //call "getNameAttribute"

Comments

0

Take a look at this and let me know is it your requirement or not.

$methods = ['name', 'city'];
$func = 'get'.$methods[1].'Attribute';
echo $func($methods[1]);

function getNameAttribute($func_name){
    $another_func = 'get'.$func_name;
    echo 'Name: '.$another_func();
}

function getCityAttribute($func_name){
    $another_func = 'get'.$func_name;
    echo 'City: '.$another_func();
}

function getCity(){
    return 'Dhaka';
}

function getName(){
    return 'Frayne';
}

Comments

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.