<?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