Why PHP is the best language (a)--strange namespaces

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.