PHP Static keyword Practical Method _php Foundation

Source: Internet
Author: User
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
?>
Related Article

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.