For compatibility with PHP4, if "visibility" is not specified, properties and methods are public by default.
The pseudo variable $this is not available in static methods because the static method does not need to be invoked through the object.
Static properties can also be accessed by an object through the-> operator.
Using:: Calling a non-static method can result in a 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 you can't point to another variable or function return value, or point to an object.
After PHP5.3.0, we can use a variable to invoke the class dynamically. However, the value of the variable cannot be the keyword self, parent, or static.
Copy Code code as follows:
<?php
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"; PHP 5.3.0 can be invoked dynamically after
Print Bar:: $my _static. "\ n";
$bar = new Bar ();
Print $bar->foostatic (). "\ n";
?>
The static keyword is used in PHP to define the properties and methods.
instance One: A method of referencing static properties
Copy Code code as follows:
<?php
/*
*author:ajax123
*qq:283400245
*/
Class person{
Static$name= "ajax123";//static declaring static properties
Static$age=25;//static declaring static properties
static$address= "Beijing";//static declaring static properties
function Song () {
echo "My name is:". Self:: $name. <br> inside Class: Accessing static properties through the Self class
echo "I am". Self:: $age. " <br> inside Class: Accessing static properties through the Self class
echo "I live in". Self:: $address. " <br> inside Class: Accessing static properties through the Self class
}
}
Echoperson:: $name. " <br> outside of class: Accessing static properties through class name person
Echoperson:: $age. " <br> outside of class: Accessing static properties through class name person
Echoperson:: $address. " <br> outside of class: Accessing static properties through class name person
?>
Example Two: A method for referencing static methods
Copy Code code as follows:
<?php
/*
*author:ajax123
*qq:283400245
/
CLA SS person{
Static$name= "ajax123";//static declaring static properties
Static$age=25;//static declaring static properties
static$address= "Beijing";/ Static declares the
Staticfunction song () {//Declare static method song
echo "My name is:". Self:: $name. <br> internal: Accessing static properties through the Self class
echo "I am". Self:: $age. <br> internal: Accessing static properties through the Self class
echo "I live in". Self:: $address. <br> internal: Accessing the static Property
}
}
Person::song () through the Self class. <br> external: Static method access through class name
?>