PHP static classes and non-static classes

Source: Internet
Author: User
Static class: A class that contains a static definition of the Static PropertiesOr the class of the method

Static Class access Methods:

1, the static method does not need to instantiate the object, can be called directly through the class name, the operator is a double colon:
Car::getname ();
2. External access to public properties or methods:
$car->speed; $car->speedup ();
3. Internal access to public properties and methods:
$this->speed; $this->speedup ();
if (Empty ($articleclass _id)) $this->showapp (Array (' msg ' = ' wrong operation '));
4. External access to static properties or methods:
Car::getname (); Car:: $price;
$articleclass _id = sutil::getstr ($_get[' id '), ' int ');

5. Internal access static properties :

Self:: $price;

6. When inheriting a class, the child class internally calls the parent class static property :

Parent:: $price;

Class Controller_article extends Controller_basepage {    function __construct () {        parent::__construct ();    }
}

7, if the non- static method , the need to change the method is not used $this, that is, no non-static variable/method is called, of course, the call static variable/method no problem.

8. Use $object-> and use class:: ... What is the difference:
1. Use $object->.., you need to execute the constructor to create the object;
2. Use class:: ... Call static method /variable, do not need to execute constructor to create object;
3. Use class:: ... Call a non- static method /variable and do not need to execute the constructor to create the object.

Why should there be static class????????

--------a static variable, or a function that is stored in static memory, is not released until the end of the program, so when did he assign the value?

When compiling, dynamic classes are dynamically allocated when the program is running.

If the class is called once, static classes need to do more work at compile time, dynamic classes need to do more work when executing, however, PHP is a dynamic language, each time these two steps are not lost, so for the class only run once, no matter who fast who slow.

But if a class in the program to multiple calls is not the same, the static class is compiled when the assignment, the program after the run can be directly called over, without the dynamic allocation of memory, saving time, which is why static analogy dynamic class fast reason (if the call many times remember).

The above describes the PHP static class and non-static classes, including static methods, static properties of the content, I hope that the PHP tutorial interested in a friend helpful.

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