0

So for example i have this class in two different ways:

class Text {
    private $Foo;

    function Bar() {
        if ($this->Foo == "Bar" || $this->Foo == "Foo") {
            return $this->Foo;
        }
    }
}


class Text {
    private $Foo;
    function Bar() {
        $Fooo = $this->Foo;
        if ($Fooo == "Bar" || $Fooo == "Foo") {
            return $Fooo;
        }
    }
}

Which way is better,faster,more secure?

Thanks!

Arjuna Wenzel
  • 142
  • 1
  • 10

2 Answers2

0

PHP performance: $this->variable versus local $variable (manipulating)

Note though that in your example the class variable is already defined, so either remove it and use the local variable or remove the local variable and use the class one. Also, use the class variable if it will need to be accessed by other classes (you'd need to make it public) or functions.

Community
  • 1
  • 1
RuubW
  • 556
  • 4
  • 17
0
  1. Use constants to be sure what you compare your variable against and to not be able to make typos

    const BAR = "bar" const FOO = "foo"

  2. Use triple equal comparator to compare if the values are equal and of the same type

    $this->Foo === BAR

  3. You're in a class, so stick to class variables if you need later access or if you use them in multiple methods.