PHP static classes and non-static classes

WBOY
Release: 2016-07-29 09:13:23
Original
802 people have browsed it

Static class: A class that contains statically defined static propertiesor methods

Static class access methods:

1,, static methodsNo need to instantiate objects, they can be called directly through the class name, the operator is 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 static properties or methods:
Car::getName();Car::$price;
$articleclass_id = SUtil::getStr($_GET['id'], 'int');

5 , Internal access to static properties:

self::$price;

6. When inheriting a class, the subclass internally calls the parent class static properties:

parent::$price;

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

7, If it is a non-static method, you need to change the method so that $this is not used, that is, non-static variables/methods are not called. Of course, there is no problem in calling static variables/methods.

8. What are the differences between using $object->… and using class::…:
1. When using $object->…, you need to execute the constructor to create an object;
2. Use class::... to call static methods/variables, and there is no need to execute the constructor to create objects;
3. Use class::... to call non-static methods/variables, and there is no need to execute the constructor to create objects.

Why do we need static classes? ? ? ? ? ? ? ?

--------Static variables or functions are saved in static memory and will only be released when the program ends. So when is it assigned?

It is during compilation. Dynamic classes are dynamically allocated when the program is running.

If called once in a class, static classes need to do more work when compiling, and dynamic classes need to do more work when executing. However, PHP is a dynamic language, and these two steps are not lost every time, so for only running once It doesn't matter who is faster or slower.

But it’s different if a class needs to be called multiple times in the program. The static class is assigned a value when it is compiled. It can be called directly when the program is running, instead of dynamically allocating memory, which saves time. , which is why static classes are faster than dynamic classes (provided that they are called multiple times and remembered).

The above introduces PHP static classes and non-static classes, including static methods and static attributes. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template