Php static and non-static classes

Source: Internet
Author: User
: This article mainly introduces php static and non-static classes. For more information about PHP tutorials, see. Static class: the class contains static defined Static attributesOr Method class

Static class access method:

1 ,,Static methodYou do not need to instantiate an object. you can call the object directly by using the class name. The operator is double colon ::
Car: getName ();
2. public attributes or methods for external access:
$ Car-> speed; $ car-> speedUp ();
3. public attributes and methods for internal access:
$ This-> speed; $ this-> speedUp ();
If (empty ($ articleclass_id) $ this-> showapp (array ('MSG '=> 'wrong operation '));
4. external accessStatic attributesOr method:
Car: getName (); Car: $ price;
$ Articleclass_id = SUtil: getStr ($ _ GET ['id'], 'int ');

5. internal accessStatic attributes:

Self: $ price;

6. when the class is inherited, the child class calls the parent class.Static attributes:

Parent: $ price;

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

7. if it is notStatic method$ This is not used in the method to be modified, that is, non-static variables/methods are not called. of course, there is no problem in calling static variables/methods.

8. use $ object->... And use class ::... Are there any differences:
1. use $ object->... , You need to execute the constructor to create an object;
2. use class ::... CallStatic method/Variable. you do not need to execute the constructor to create an object;
3. use class ::... Call non-Static method/Variable, and you do not need to execute the constructor to create an object.

Why is there a static class ????????

-------- Static variables or functions are stored in the static memory. they will be released only when the program ends. When will they be assigned a value?

During compilation, dynamic classes are dynamically allocated when the program is running.

If the class is called once, the static class needs to do more work during compilation and the dynamic class needs to do more work during execution. However, php is a dynamic language, every two steps are not lost. Therefore, for a class that runs only once, it doesn't matter who is going to be slow.

However, if a class needs to be called multiple times in the program, the static class is assigned a value during compilation and can be called directly after the program is run, instead of dynamically allocating memory, it saves time, which is why static analogy is fast (provided that you remember to call it multiple times ).

The above introduces php static and non-static classes, including static methods and static attributes, and hopes to help those who are interested in PHP tutorials.

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.