PHP calling Scope

Source: Internet
Author: User

    • Author: laruence ()

    • This address: http://www.laruence.com/2012/06/14/2628.html

Yesterday in the YAF Exchange Group, the Prairie classmate criticized me to become lazy, blog for a long time did not update, today just someone in the segmentfalut asked me a question, I also did a simple answer on the micro-Bo, but feel a word is not clear, write a blog number bar.

The problem here, because it is too long, I will not copy it: This is PHP __call and __callstatic in the inheritance will be generated after the bug?

At first glance, this problem is really confusing, but in fact, the root cause of this misunderstanding is: in PHP, the judge is not static or not by ":" (Paamayim_nekudotayim) symbol, but by calling scope.

So, what is calling scope?

In PHP, when a method is called, the object that the $this pointer to is the calling scope at which the method is called. For the following example:

<?phpfoo::bar ();? >

When the bar method is called, it is in a context that does not have a calling scope domain, so this is a static call.

And for the following example:

<?phpclass A {public Function test () {Foo::bar (); }} $a = new A (); $a->test ();

When calling the bar method, in the context of a $ A $ object, that is, the calling scope at this time is a $ A object, so this is not actually a static call.

To verify this conclusion, consider one of the following practical examples:

<?php class Foo {public Function bar () {var_dump ($this);     }} class A {public function test () {Foo::bar (); }} $a = new A (); $a->test ();? >

What does it output?

Object (A) #1 (0) {}

In the call to bar, this seemingly "static" invocation of the call, $this pointer is assigned value, point to a $ A object, then this is static call it?

I cite this example to illustrate this problem, but in practical applications, we try to avoid using "::" to invoke a non-static method, PHP will also give a strict warning for such a call:

Strict Standards:non-static Method Foo::bar () should not being called statically, assuming $this from incompatible context

Maybe someone would say this should be a bug, right? In fact, more should be caused by wrong use, because you invoke a non-static method of a class in the context of a calling scope with a "static form".

So why is PHP so designed? Consider the following example:

<?php Class A {public function __construct () {}} class B extends A {public Function __construct () {   Parent::__construct (); }   }

When we call the constructor of the parent class, we deliberately pass the current scope to the parent class's constructor as calling scope.

Now, do you have a little more understanding of static calls? In the afternoon the company will open the plenary meeting, hurriedly and on, write may be a bit messy, please everyone haihan, hehe, thanks


PHP calling Scope

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.