PHP static method

Source: Internet
Author: User

<?php
Class book{
public $name;//variables
public static $price;//static variable
Const id=1;//Constants
function SetName ($name) {//member method
$this->name= $name;//Access normal variable You can use the This keyword
}
function GetName () {
return $this->name;
}
function Setprice ($price) {
Self:: $price = $price;//Accessing a static variable requires using the self::$ variable name to access the value of the variable
}
function GetPrice () {
Return self:: $price;//static variable is invoked only with self, not with this
return self::id;
}
static function GetID () {
The return self::id;//constant is called to access the constant using the self:: constant name
}
}


book::id;//class external access constants and internal similarities, self becomes the class name
$book =new Book ();
Book:: $price//access to static variables
$book->getid ();//static method can be called by object
Book::getid ();//static methods can also be called directly through the class

Summarize:
1. Accessing static variables and methods inside a class access constant with self::$ variable name self :: constant Name
2. The This keyword cannot be used in static methods
3. Variables can only be called with this within the class and cannot be used with self
4. Non-static variables cannot be called inside a static method, method
5. Static methods can be called inside or outside a class by using self:: And this method


Php static method

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.