static properties and static methods for PHP static

Source: Internet
Author: User

First, static properties

A static property cannot be accessed by an object through the operator.

A static property can only be initialized to literal or constant, and cannot be used with an expression. You can initialize a static property to an integer or an array, but you cannot initialize it to another variable or function return value, or point to an object.

How do I access static properties?

Inside the class: Class Name:: $ static property or self::$ static property

Outside the class: class Name:: $ static Property or object instance:: $ static property

classperson{Public Static$nums =0;//declaration of static Properties     Publicfunction Addnum () {self:: $nums++;   echo self:: $nums; //inside the class, use the Self keyword to access the static propertyecho Person:: $nums;//class can access static properties directly//Echo $this->nums; //error, static properties cannot be accessed by the object by operator}} $p 1=NewPerson (); Echo $p 1:: $nums; //object instances can access static properties like this

Second, static method

Static methods are used to manipulate static properties. Therefore, non-static properties cannot be manipulated inside a static method.

because static methods do not need to be called through an object, the pseudo-variable $this in a static method does not available.

How do I access static methods?

Inside the class: class Name:: Static method or Self:: Static method

Outside the class: class Name:: Static method or object instance-static method

classperson{ Public Static$nums =0;//declaration of static Properties     Public$age =0;  Publicfunction Addnum () {self:: $nums++;   echo self:: $nums; //inside the class, use the Self keyword to access the static propertyecho Person:: $nums;//class can access static properties directly//Echo $this->nums; //error, static properties cannot be accessed by the object by operatorSelf::getnum ();//inside the class, use the Self keyword to access the static methodPerson::getnum ();//class can access static methods directly    }     Public Staticfunction Getnum () {//Static Methods//Echo $this->age; //error, static method inside cannot manipulate non-static propertyEcho'Are you sure? '. Self:: $nums;//Static properties can only be manipulated in static methods}} $p 1=NewPerson (); $p 1-addnum (); Echo $p 1:: $nums; //object instances can access static properties like thisPerson::getnum ();//class can access static methods directly$p 1->getnum ();//object instances can access static methods like this

static properties and static methods for PHP static

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.