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(); }
}
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.
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.