Classes and objects in PHP: Static properties, static methods and graphics

Source: Internet
Author: User
This article mainly introduces the classes and objects in PHP: Static properties, static methods, the need for friends can refer to the following

In PHP, using the keyword static modifier member properties and member methods are called static properties and static methods. static properties and static methods do not need to be used directly in cases where the class is instantiated.

Static properties

Unlike normal member properties, a static property belongs to the class itself and not to any instance of the class. Static properties can be seen as global variables stored in classes that can be accessed from anywhere through classes.

Because static properties are not constrained by any specific object, you do not need to establish a class instance, but you can directly reference a static property in the class, using the syntax format as follows:

Class Name:: $ static property name

Where the symbol "::" is called the scope-resolution operator, which can be used to access static members, static methods and constants, and to override members and methods in a class.

If you want to access a static property in a member method of the same class, you can do so by adding the operator "Self::" Before the name of the static property. Examples are as follows:


Class dabing{  static $bing =0;   Define static properties Bing  function AddOne () {self    :: $bing + +;    Call a static property in the same class Bing    echo "\ $bing has the value:". Self:: $bing + +;  }} $obj =new dabing (); $obj->addone ();


This program causes the value of the static property to be added 1, and the result is: 1

Static methods

Declaring a member method static in PHP is called a static method. Because static methods are not constrained by any specific object, you do not need to establish a class instance to directly reference a method in a class. The syntax format is as follows:

Class Name:: Static method Name ([parameter 1, Parameter 2, ...] )
The same as calling a static property, using the Operator "self::" In a class to represent the invocation of a static method in the same class, as in the following example:


Class dabing{static  function AddOne ($number) {//define static method AddOne ()    echo "\ $number +1=";    echo $number +1;  }  static function Showresult ($number) {    echo "\ $number =". $number;    echo "<br>";    Self::addone ($number); Call static methods in the same class AddOne ()  }} $number =100;dabing::showresult ($number);


The result of the program execution is:

$number =100
$number +1=101


The above is the whole content of this article, I hope that everyone's study has helped.


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.