Home  >  Article  >  Backend Development  >  Practical methods of php Static keyword_PHP tutorial

Practical methods of php Static keyword_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:37:08803browse

For compatibility with PHP4, if "visibility" is not specified, properties and methods default to public.
Since static methods do not need to be called through objects, the pseudo variable $this is not available in static methods.
Static properties can also be accessed by objects through the -> operator.
Calling a non-static method using the :: method will result in an E_STRICT level error.
Like all other PHP static variables, static properties can only be initialized to a character value or a constant, and expressions cannot be used. So you can initialize a static property to an integer or an array, but it cannot point to another variable or function return value, nor to an object.
After PHP5.3.0, we can use a variable to dynamically call a class. But the value of this variable cannot be the keywords self, parent or static.

Copy code The code is as follows:

class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;
}
}
print Foo::$my_static . "n";
$foo = new Foo();
print $foo->staticValue() . "n";
print $foo->my_static . "n"; // Undefined "Property" my_static
print $foo::$my_static . "n" ;
$classname = 'Foo';
print $classname::$my_static . "n"; // After PHP 5.3.0, you can dynamically call
print Bar::$my_static . "n";
$bar = new Bar();
print $bar->fooStatic() . "n";
?>

Static is used in PHP Keywords to define static properties and methods.

Example 1: Reference method of static properties
Copy code The code is as follows:

/*
*author:ajax123
*qq:283400245
*/
class person{
static$name="ajax123";//static declares static properties
static$age=25;//static declares static properties
static$address="Beijing";//static declares static properties
function song(){
echo "My name is : ".self::$name."
";//Internal class: access static properties through the self class
echo "I am ". self::$age."
";//Inside the class: access static properties through the self class
echo "I live in ".self::$address."
";// Inside the class: Access static properties through the self class
}
}
echoperson::$name."
"; // Outside the class: Access static properties through the class name person
echoperson: :$age."
";//External to the class: Access static properties through the class name person
echoperson::$address."
";//External to the class: Access static properties through the class name person Attributes
?>


Example 2: Reference method of static method

Copy code The code is as follows:

/*
*author:ajax123
*qq:283400245
*/
class person{
static$name="ajax123";//static declares static attributes
static$age=25;//static declares static attributes
static$address="Beijing";//static declares static Attribute
staticfunction song(){ //Declare static method song
echo "My name is : ".self::$name."
";//Internal class: access static through self class Property
echo "I am ".self::$age."
";//Internal class: access static properties through the self class
echo "I live in ".self::$address ."
";//Inside the class: access static properties through the self class
}
}
person::song()."
";//Outside the class: through Class name person accesses static method
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322005.htmlTechArticleIn order to be compatible with PHP4, if "visibility" is not specified, properties and methods default to public. Since static methods do not need to be called through objects, the pseudo variable $this cannot be used in static methods...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn