There is a section in the official PHP documentation:
1<?PHP2 classFoo {3 var $bar= ' I am bar. ';4 }5 6 $foo=Newfoo ();7 $bar= ' Bar ';8 $baz=Array(' foo ', ' Bar ', ' baz ', ' Quux '));9 Echo"{$foo-$bar}<br? ";Ten Echo"{$foo-$baz[1]} <br> "; One?>
The result of the code output is as follows:
I am Bar.
I am Bar.
1<?PHP2 classFoo {3 var $bar= ' I am bar. ';4 var $baz= ' You yyy. ';5 }6 7 $foo=Newfoo ();8 $bar= ' Bar ';9 $baz=Array(' foo ', ' Bar ', ' baz ', ' Quux '));Ten Echo"{$foo-$bar}<br> "; One Echo"{$foo-$baz[1]} <br> "; A Echo"{$foo-$baz[2]} <br> "; -?>
The result is output:
I am Bar.
I am Bar.
You yyy.
Confirmation $foo-$baz [2] is $foo->baz,
Similarly $foo $baz [1] is $foo->bar,
So the eighth line here $bar = ' bar '; What role does it have? Get rid of the try
Get the error prompt:
Notice: Undefined variable:bar in C:\phpStudy\PHPTutorial\WWW\sb.php on line
Fatal Error: Cannot access empty property in C:\phpStudy\PHPTutorial\WWW\sb.php on line
It was assigned to $bar $foo, in line 10th ,
So the $bar in the Foo () class and the $bar on the 6th row outside are two addresses, although the same name does not interfere with each other.
Verify that:
1<?PHP2 classFoo {3 var $bar= ' I am bar. ';4 var $baz= ' You yyy. ';5 }6 7 $foo=Newfoo ();8 $aaa= ' Bar ';9 $baz=Array(' foo ', ' Bar ', ' baz ', ' Quux '));Ten Echo"{$foo-$aaa}<br> "; One Echo"{$foo-$baz[1]} <br> "; A Echo"{$foo-$baz[2]} <br> "; -?>
View results:
I am Bar.
I am Bar.
You yyy.
As with the second code result, the conjecture is correct and the validation succeeds.
Why PHP is the best language (a)--strange namespaces