Comparison of single and static methods in object-oriented PHP, and analysis of class auto-loading

Source: Internet
Author: User
static method:

Example

Class A{public static function A () {# code ...} public static function B () {# code ...}} Use A::a (); A::b ();


When the script is interpreted, the static method is loaded into memory (and a single copy of the storage). Use it as if you were using a function.

Single Example:

In order to achieve a class in memory only a single copy is stored, with static variables through code implementation of a design pattern

Example

Class Container () {protected Static $loadedSingletonClasses = [];p ublic static function loadsingletonclass ($className = ' ') {if (!isset (self:: $loadSingletonClass [$className])) {self:: $loadSingletonClass [$className] = new $className;} Return self:: $loadSingletonClass [$className];}} $a = Container::loadsingletonclass ("Foo\bar\myclass"); $b = Container::loadsingletonclass ("Foo\bar\myclass");


In the above code $a the $b variable points to the same memory address, (but if you want to trigger both variables to instantiate the class's destructor needs to destroy both variables, see PHP Object-oriented knowledge summary)

One thing that differs from static methods is that static methods are loaded into memory when the script is interpreted, and the singleton is loaded into memory only when new is available (provided the code for both is loaded into the memory code area)

Auto Load:

There is an explanation of the automatic loading in front (AutoLoad implementation Mechanism in PHP)

When we

Instantiate class new Class

Call static method Class::func ()

Inheriting classes, interfaces subclass extends parentclass{}

This will trigger the auto-load function:

When we alias a class, the class name passed is also the class before the alias.

The above describes the PHP object-oriented single and static methods of comparison, and about class automatic loading analysis, including the aspects of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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