Php Static keywords

Source: Internet
Author: User

To ensure compatibility with PHP4, if "visibility" is not specified, the attribute and method are considered public.
Because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods.
Static attributes can also be accessed by objects through the-> operator.
Use: When a non-static method is called, an E_STRICT error occurs.
Like all other PHP static variables, static attributes can only be initialized as a character value or a constant, and expressions cannot be used. Therefore, you can initialize a static property as an integer or array, but it cannot point to another variable or function return value or to an object.
After PHP5.3.0, we can use a variable to dynamically call the class. However, the value of this variable cannot be self, parent, or static.
Copy codeThe Code is 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"; // can be dynamically called after PHP 5.3.0
Print Bar: $ my_static. "\ n ";
$ Bar = new Bar ();
Print $ bar-> fooStatic (). "\ n ";
?>

Use the Static keyword in PHP to define Static attributes and methods.

Example 1: static attribute Reference Method
Copy codeThe Code is as follows:
<? Php
/*
* Author: ajax123
* Qq: 283400245
*/
Class person {
Static $ name = "ajax123"; // declare static attributes
Static $ age = 25; // declare static attributes
Static $ address = "Beijing"; // declare static attributes
Function song (){
Echo "My name is:". self: $ name. "<br>"; // class: Access static attributes through the self class
Echo "I am". self: $ age. "<br>"; // class: Access static attributes through the self class
Echo "I live in". self: $ address. "<br>"; // class: Access static attributes through the self class
}
}
Echoperson: $ name. "<br>"; // class exterior: Access static attributes by class name person
Echoperson: $ age. "<br>"; // class exterior: Access static attributes by class name person
Echoperson: $ address. "<br>"; // class exterior: Access static attributes by class name person
?>


Example 2: static method reference

Copy codeThe Code is as follows:
<? Php
/*
* Author: ajax123
* Qq: 283400245
*/
Class person {
Static $ name = "ajax123"; // declare static attributes
Static $ age = 25; // declare static attributes
Static $ address = "Beijing"; // declare static attributes
Staticfunction song () {// declare static method song
Echo "My name is:". self: $ name. "<br>"; // class: Access static attributes through the self class
Echo "I am". self: $ age. "<br>"; // class: Access static attributes through the self class
Echo "I live in". self: $ address. "<br>"; // class: Access static attributes through the self class
}
}
Person: song (). "<br>"; // class External: Access static methods by class name person
?>

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.