language design - What is the point of having $this and self:: in PHP? -


Why do you need to explicitly write $ this ? I used to understand that you had to use $ this :

  function fu ($ bar) {$ this- & gt; Bar = $ bar; }  

But you should explicitly write it in the verbose code which looks like this:

  $ this-> Var3 = globalFun ($ this-> var, $ this-> var2 [$ this-> activation], $ this-> method ()); Contrary to  

:

  $ var3 = globaFun ($ var, $ var2 [$ anotherVar], method ());  

So what does $ this mean?

Additional Bonus Question:

Why do we have to do this to isolate static references and examples? Why do we need:

  static function getValue () {returns self :: value; }  

Can not know on PHP / runtime if the query in the variable / method is static? Now if I want to change one method to static by non-static, then I have to replace all those self :: with $ this-> / Code> (and vice versa).

It would not be better if we have $ this which behaves like this in Java?

OK, we remove the need to type $ this everywhere Take a look at this situation:

  square fu {public function setbar ($ value) {$ bar = $ value; }} $ Foo = New Foo (); $ Foo- & gt; Set-up ('some value');  

Is a local variable or a member of $ foo ?

Should There Be Different Discrimination? They could allow declaration of local variables with the keyword var , but it is not backwards compatible and is very confusing to people upgrading from previous versions of PHP.

The same thing applies to self :: how interpreter knows what the function you want to call is specific to the global or class?


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -