0

Assume I have an object with 3 properties:

protected $validMainStatements;
protected $validPrimaryStatements;
protected $validSecondaryStatements;

And I got the following method:

public function selectType($stmt) {
    $stmtParts = MainServerInterface::parse($stmt);
    $type = $stmtParts[0] //returns either Main, Primary or Secondary
}

Depending on the value of type, I want to use the associated property. A simple implementation would be:

public function selectType($stmt) {
    $stmtParts = MainServerInterface::parse($stmt);
    $type = $stmtParts[0] //returns either Main, Primary or Secondary

    if($type === "Main") {
        $usedProp = $this->validMainStatements;
    } elseif($type === "Primary") {
        $usedProp = $this->validPrimaryStatements;
    } elseif($type === "Secondary") {
        $usedProp = $this->validSecondaryStatements;
    }
}

I think I don't have to mention that this is ugly and uncomfortable to use. Is there a way to implement this in an easier way? Something like (pseudocode):

$usedProp = $"valid".$type."Statements";

3 Answers 3

3
<?php
class Foo {
    protected $validMainStatements = 1;
    protected $validPrimaryStatements = 2;
    protected $validSecondaryStatements = 3;

    public function bar() {
        $type = 'Primary';

        return $this->{'valid'.$type.'Statements'};
    }
}

$foo = new Foo;
echo $foo->bar();

see Variable variables - Example #1 Variable property example

-- edit and btw: I'd rather do it this way:

<?php
class Foo {
    protected $validStatements = [
        'Main' => 1,
        'Primary' => 2,
        'Secondary' => 3
    ];

    public function bar() {
        $type = 'Primary';

        return $this->validStatements[$type];
    }
}

$foo = new Foo;
echo $foo->bar();
Sign up to request clarification or add additional context in comments.

1 Comment

Alright, i like your 2nd solution, that's really elegant. Sold. Thank you.
1

Demo

Try like below

$usedProp = $this->{"valid".$type."Statements"};

Test

<?php

   class test {
         public $validMainStatements = 'hello';
   }

  $instance = new test;

  $type = 'Main'; 
  echo $instance->{"valid".$type."Statements"};

?>

Result

hello

6 Comments

The valid and Statements (the ones inside the {} in the echo statement) need to be enclosed in quotes!
@someOne : thanks, edited now as you suggested, but didn't understand why I didn't get error in eval.in/, just look at demo
@someOne Absolutely, it's very dangerous relying on assumed strings - always enclose in " or '
Most probably because the eval.in has turned the warnings off and uses the most recent versions of PHP (which have the feature of making assumptions)!!
|
0

Just use variable variables.

$variableName = 'valid'.$type.'Statements';
$this->{$variableName};

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.