Php static method call non-static steps are not available

Source: Internet
Author: User
Php static method call non-static method is not available that day to be more interested in this topic, so I checked the relevant information to record it. However, I don't know whether there are any omissions in my writing, so I will send them here to serve as targets for everyone. If you find anything that is incorrect or unreasonable, make a brick-of course accept tomatoes and eggs. Truth: only php static methods call non-static methods.

I was very interested in this topic that day, so I checked the relevant information to record it. However, I don't know whether there are any omissions in my writing, so I will send them here to serve as targets for everyone. If you find anything that is incorrect or unreasonable, make a brick-of course accept tomatoes and eggs. Truth exists only in debate.

Static call of non-static methods in php

1 Static method vs Non Static method?

2 PHP static method performance

Php is a very strange language. Of course, this is for people who have learned C ++, Java, and other object-oriented languages.
Php can define static methods and call them in the form of className: staticMethod. Non-static methods, of course, are used through classObject-> nonStaticMethod. The same is true in other languages. However, can you use className: nonStaticMethod () to call non-static methods? In this regard, I am afraid Java and C ++ will be cold and sweaty. In fact, php itself is also full of black lines. Why?
Let's take a look at Object-Oriented static and non-static. Static methods are supported in object-oriented languages. The static method belongs to the fixed assets of the class; the non-static method belongs to the private property of the instance of the class. In the memory, static methods only store such a copy for the entire class. no matter how many new instance objects you have, they share the same copy. It is different for non-static ones. if you have a few new ones, the memory will give you a few new ones. In addition, non-static methods cannot be called in static methods, but static methods can be called in non-static methods. This is the difference between static and non-static.
Objects are defined with the static keyword. A static method cannot be called by adding two colons to the class name. The difference between php and others is that php is not labeled as a static method, and can also be called by adding two colons to the class name. So why does php have this syntax? Why do you feel helpless?
----- The following describes the helpless story of php, which has been adapted based on relevant data --------
The rapid development of php has benefited from its speed. As a scripting language, php pursues high speed and conciseness and convenience, so it chooses to explain the execution and use process methods. Later, in order to be in line with international standards, the concept of object-oriented was introduced. While introducing this object-oriented feature, php was stunned and ultimately helpless. An important concept of object-oriented is inheritance. In inheritance, if a subclass overwrites the method of the parent class and needs to call the method of the same name as the parent class, what should I do? Php4 provides the following method: parentClassName: method (). When this method was proposed, people of insight had already found the problem: Isn't this call method the static method? Php4 then replied: No. When using the class, you can determine that this method is a subclass that is calling the parent class method; when using a non-class, you can determine that it is a static call. All you need is to query the method ING for this call. In fact, this is also the case. Php4 later thought about it. if every call checks whether the call is legal, the performance will be affected. if this problem is not solved, let the programmer control it: they will know that this form of call is only used in the class. Alas, it's a pity that we don't have enough people to do that. Php4 underestimated the creativity of programmers! Programmers quickly discovered this method and made no effort to use it. Many integrated APIs are also beginning to use this strange method. Php is helpless, and another method is introduced in php5. the parent keyword is used to call the parent class function: parent: method (). However, it is no longer possible to discard the static call of php non-static methods.
However, what are the advantages of this strange php method? What about performance and memory usage?
So I began to Reason: I defined a non-static method. During static calls, php first converted this method to a static definition, loaded the static memory area, and then executed it. Generally, only one method in one service processing class is used for a business. if non-static definition and static call are used, it is not necessary to load only one method in this service class in the memory, didn't I implement on-demand loading of static methods? Isn't it necessary to save some memory? In terms of performance, whether it is static call or object call, a method is executed, and the performance is not the same? In addition, the non-static definition method called statically saves a new statement. Well, well. At the same time, I started writing my hand.
What is the actual situation? I did a small test.

PHP code
T: start (); t: end (); // eliminate the impact of the first loading of the t class t: start (); model_profile_base: getBaseInfo ($ uid ); t: end (); t: start (); $ model = new model_profile_base (); $ model-> getBaseInfo ($ uid); t: end ();

Model_profile_base is a business class that processes basic data. it is complex and close to the actual business processing in the project.
The following is the definition of the t class used for timing and memory statistics:

PHP code
  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 consumed :'. (self ::$ end_time-self ::$ start_time ). ', shared memory :'. (self ::$ end_memory-self ::$ start_memory );}}

The result is as follows:

PHP code
Start @ 1287561987.1805 (1008368) | -------> End @ 1287561987.1806 (1008368): |======== total time consumption: 3.2901763916016E-5, shared memory: 0 Start @ 1287561987.1806 (1008368) | -------> End @ 1287561987.1938 (1586452): |======== total time consumed: 0.013248920440674, shared memory: 578084 Start @ 1287561987.1938 (1586452) | -------> End @ 1287561987.1945 (1586652): | ======= Total time consumption: 0.00065183639526367, shared memory: 200

The second line is static call of non-static methods, and the third line is normal call of non-static methods. Then I found my reasoning tragedy. After several page refreshes, the statistical results are similar in order of magnitude. Static call non-static methods are not flattering in terms of memory usage or performance. This result is a bit confusing.
Then, try again the result of loop execution multiple times:

PHP code
T: start (); t: end (); // eliminate the impact of the first loading of the t class t: start (); for ($ I = 0; $ I <1000; ++ $ I) model_profile_base: getBaseInfo ($ uid); t: end (); t: start (); $ model = new model_profile_base (); for ($ I = 0; $ I <1000; ++ $ I) $ model-> getBaseInfo ($ uid); t :: end ();

As a result, even more speechless results came out:

PHP code
Start @ 1287562243.5799 (1009372) | -------> End @ 1287562243.5799 (1009372): |======== total time consumption: 3.0040740966797E-5, shared memory: 0 Start @ 1287562243.58 (1009372) | -------> End @ 1287562244.1532 (1587544): |======== total time consumed: 0.57321000099182, shared memory: 578172 Start @ 1287562244.1532 (1587544) | -------> End @ 1287562244.6921 (1587744): | ======= Total time consumption: 0.53887605667114, shared memory: 200

In addition to the two methods that start to be close at the same time (and the normal call is still relatively advantageous), the memory is still quite different. Disappointed, I checked the internet and found that some people did similar tests. I will simply copy the result:
(May look at the results, it will feel a little difficult to understand, you can find the detailed description here:

Test result (order by time DESC ):

PHP code
============Which method========================Time======Inline calculation                             0.0805 sNormal function call                           0.3438 sNormal method called through object            0.4118 sStatic method called statically                0.4280 sUnspecified method called through object()     0.4294 sUnspecified method called statically()         0.6960 s

In this case, the non-static method of static call is not dominant in both performance and memory. In addition, this method is prone to maintenance confusion. So, let's take a short and strong conclusion: static call is not desirable for non-static methods. [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: 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.