When would you use the $this keyword in PHP?

前端 未结 12 1730
长发绾君心
长发绾君心 2020-12-15 11:03

When would you use the $this keyword in PHP? From what I understand $this refers to the object created without knowing the objects name.

Al

12条回答
  •  北海茫月
    2020-12-15 11:39

    A class may contain its own constants, variables (called "properties"), and functions (called "methods").

    var;
        }
    }
    ?>
    

    Some examples of the $this pseudo-variable:

    foo();
    
    // Note: the next line will issue a warning if E_STRICT is enabled.
    A::foo();
    $b = new B();
    $b->bar();
    
    // Note: the next line will issue a warning if E_STRICT is enabled.
    B::bar();
    ?>
    

    The above example will output:

    • $this is defined (A)
    • $this is not defined.
    • $this is defined (B)
    • $this is not defined.

提交回复
热议问题