PHP static method call non-static step not fetching

Source: Internet
Author: User
PHP static method call non-static method is not advisable

The topic was more interesting that day, so I looked up the relevant information to describe it. However, do not know that they have written in the end there is no omission, so, sent here to give everyone a target. If you find any narrative incorrect or unreasonable, enjoy making bricks--and of course accept tomatoes and eggs. Truth exists only in the debate.

---------------------------------------------
static invocation of non-static methods in PHP

Resources:
1 static method vs Non static method?
Http://bytes.com/topic/php/answers/495206-static-method-vs-non-static-method

2 PHP static method performance
Http://vega.rd.no/articles/php-static-method-performance


PHP is a very strange language. Of course, this is for people who have studied object-oriented languages such as C + + or java.
PHP can define a static method and then call it through the Classname::staticmethod () Form. Non-static method, of course, is used by Classobject->nonstaticmethod (). The same is true in other languages, no fuss. However, can you call non-static methods with Classname::nonstaticmethod ()? I am afraid that Java and C + + will be cold Sweat DC. In fact, PHP itself is also full face black line. Why do you say that?
Let's take a look at the object-oriented static and non-static. In object-oriented languages, static methods are supported. A static method that belongs to a class of fixed assets; a non-static method that belongs to the private property of an instance of a class. In memory, a static method has only one copy of the entire class; no matter how many instances you have new, they share the same share. For non-static is not the same, you new, memory will give you new several. In addition, non-static methods can not be called inside a static method, but static methods can be called inside a non-static method. This is the difference between static and non-static.
Object-oriented defines static with the static keyword. A method that is not marked as static is not invoked in the form of a class name plus two colons. PHP and other very different point is this: PHP is not marked as a static method, you can also use the class name plus two colons in the form of a call. So why does PHP have this syntax? Why do you feel so helpless?
-----The following instructions PHP helpless story according to the relevant information adapted, has been treated by the kingdoms--------
PHP has evolved so quickly, thanks to its speed. As a scripting language, PHP pursues high speed and simplicity, so it chooses to interpret execution and use process methods. Later, in order to conform with the international, the concept of object-oriented was introduced. And that is in the introduction of this object-oriented features, there has been an open PHP stunned, the ultimate helpless thing. An important concept of object-oriented is inheritance. In inheritance, if a subclass overrides a method of the parent class and needs to invoke the same name method of the parent class, what should I do? The PHP4 version provides a way to: Parentclassname::method (). When this approach is presented, insight has discovered the problem: Is this call method not just a static method invocation? PhP4 immediately replied: "Not in the way." When used in a class, it is possible to judge that this method is calling a parent class method for a subclass, and when used in a non-class, it is called a static call. All that is needed is to find out how to query the method mappings when this kind of call is made. In fact, a thought, it is really so. PhP4 later think, if each call to check whether the call is legitimate, that how much will affect performance, rather than this problem to the programmer to control it: they will know only in the class using this form of invocation. Alas, the day days wondering people wish. PHP4 underestimated the creativity of programmers! The programmers quickly discovered the way and used it with no force. Many integrated APIs are also starting to use this quirks approach. PHP is helpless, then introduced in php5 another way, using the keyword parent to call the parents function: Parent::method (). However, it is no longer possible to discard static calls to a non-static method of PHP.
--------------------
But, anyway, what is the advantage of this kind of PHP quirks? What about performance and memory footprint?
So I started to reason: defined a non-static method, when the static call, PHP first convert this method to static definition, load into the static memory area, and then execute. Usually once a business, using only one method in a business processing class, if the static call is used statically, does not the memory load only one of the methods in the business class, does it not implement the on-demand load of the static method? Do you want to save a bit of memory? In terms of performance, whether it's a static call or an object call, it's all about executing a method, and the performance is not the same. and statically invoking a non-static definition method also saves a new statement. Well, uh. At the same time, the hand began to write.
So how is it actually? I did a little test.

PHP Code

T::start (); t::End(); //eliminate the impact of the first load of T classT::start (); Model_profile_base::Getbaseinfo ($uid); t::End(); t::start ();$model = Newmodel_profile_base ();$model -Getbaseinfo ($uid); t::End();


Model_profile_base is a business class that deals with basic data, is more complex and is closer to the actual business process in the project.
The following is a definition of the T class used for timing and statistical memory:

PHP Code

PHPfunctionmicrotime_float () {List($usec, $sec) = Explode(" ", Microtime()); return ((float)$usec + (float)$sec);}classt{Static $start _time; Static $end _time; Static $start _memory; Static $end _memory; Public Static functionstart () { self::$start _memory =Memory_get_usage (); Self::$start _time =microtime_float (); Echo '
Start @'. Self::$start _time.'('. Self::$start _memory.') |------->'; } Public Static function End() { self::$end _time =microtime_float (); Self::$end _memory =Memory_get_usage (); Echo 'End @'. Self::$end _time.'('. Self::$end _memory.') :'; Echo '|======= Total time:'.( Self::$end _time- Self::$start _time).', shared Memory:'.( Self::$end _memory- Self::$start _memory); }}


This is called only once and the result is as follows:

PHP Code

Start @1287561987.1805(1008368)|------->End @1287561987.1806(1008368) :|=======Total time:3.2901763916016E-5, shared Memory:0Start @1287561987.1806(1008368)|------->End @1287561987.1938(1586452) :|=======Total time:0.013248920440674, shared Memory:578084Start @1287561987.1938(1586452)|------->End @1287561987.1945(1586652) :|=======Total time:0.00065183639526367, shared Memory: $


The second line is to statically call the non-static method, and the third line is to call the non-static method normally. Then I found out that my reasoning was tragic. Brush several times the page, the statistical results are similar in order of magnitude. Static calls to non-static methods are not flattering regardless of memory consumption or performance. The result is a bit raspberry.
Then, try the results of the loop execution multiple times:

PHP Code

T::start (); t::End(); //eliminate the impact of the first load of T classT::start (); for($i=0; $i< +;++$i) Model_profile_base::Getbaseinfo ($uid); t::End(); t::start ();$model = Newmodel_profile_base (); for($i=0; $i< +;++$i) $model -Getbaseinfo ($uid); t::End();



So the result is more silent:

PHP Code

Start @1287562243.5799(1009372)|------->End @1287562243.5799(1009372) :|=======Total time:3.0040740966797E-5, shared Memory:0Start @1287562243.58(1009372)|------->End @1287562244.1532(1587544) :|=======Total time:0.57321000099182, shared Memory:578172Start @1287562244.1532(1587544)|------->End @1287562244.6921(1587744) :|=======Total time:0.53887605667114, shared Memory: $


There is still a world of difference in memory, in addition to the two ways in which the time begins to approach the outside (and the normal call is relatively neat). Disappointed, checked the internet, found that someone did a similar test. I'll just cuff the results:
(may see the result, will feel a bit difficult to understand, can find the detailed explanation here: http://vega.rd.no/articles/php-static-method-performance)

Test results (ORDER by Time DESC):

PHP Code

============which method======================== Time======Inline Calculation0.0805SnormalfunctionPager0.3438Snormal method called throughObject 0.4118sStaticmethod called statically0.4280sunspecified method called throughObject() 0.4294Sunspecified method called statically ()0.6960s


In this sense, static calls to non-static methods do not have an advantage in performance and memory, and in addition, this method of invocation is prone to maintenance confusion. So, a short and powerful summary: static calls to non-static methods are undesirable. [code=php] [/code] [code=php] [/code]

  • 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.