2

i was looking at a tutorial from ZendCasts where i wondered about the code he used. a simplified version below

class TestClass {
    private $_var;
    private static function getDefaultView() {
        if (self::$_var === null) { ... } // this is the line in question
    }
}

i wonder why is something like isset(self::$_var) not used instead? when i use self:: i need the $ sign to refer to variables? i cant do self::_var? how does == differ from ===

3 Answers 3

1

These are several questions.

I wonder why is something like isset(self::$_var) not used instead

It's indifferent. The advantage of using isset is that a notice is not emitted if the variable ins't defined. In that case, self::$_var is always defined because it's a declared (non-dynamic) property. isset also returns false if the variable is null.

when i use self:: i need the $ sign to refer to variables?

Note that this is not a regular variable, it's a class property (hence the self, which refers to the class of the method). Yes, except if this is a constant. E.g.:

class TestClass {
    const VAR;
    private static function foo() {
        echo self::VAR;
    }
}

how does == differ from ===

This has been asked multiple times in this site alone.

Sign up to request clarification or add additional context in comments.

Comments

1
  1. For the == and === operators, see the manual page.
  2. For the self, see here

Comments

1

The === operator means "equal and of same type", so no automatic type casting happens. Like 0 == "0" is true, however 0 === "0" is not.

The self::$var syntax is just the syntax. use $ to refer to a variable, no $ to refer to a function.

The self:: syntax is used for static access (class variables, class methods, vs. instance variables refereed to by this).

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.