Php object-oriented? Static Member Php object-oriented? Static Member
Static attributes
Attributes declared using the static keyword
This static property is logically defined in the class. Ensure that a class corresponds to an attribute.
Example:
Class Student
{
Public $ stu_id;
Public $ stu_name;
Public static $ stu_count = 0;
Public function _ constuct ($ id, $ name)
{
$ This-> stu_id = $ id;
$ This-> stu_name = $ name;
}
}
Access static attributes:
Access through the class: in the use of static access symbols (::)
Class: Member
Example:
Class Student
{
Public $ stu_id;
Public $ stu_name;
Public static $ stu_count = 0;
Public function _ constuct ($ id, $ name)
{
$ This-> stu_id = $ id;
$ This-> stu_name = $ name;
Student: $ stu_count ++;
}
}
: The access is called static access.-> Non-static access (object access)
When you access static attributes, if you access them within the class: you can use the current class of self.
Example:
Class Student
{
Public $ stu_id;
Public $ stu_name;
Public static $ stu_count = 0;
Public function _ constuct ($ id, $ name)
{
$ This-> stu_id = $ id;
$ This-> stu_name = $ name;
Self: $ stu_count ++;
}
}
Note: the difference between $ this and self
$ This indicates this object, and self indicates this class.
Static method
The logic meaning of static methods is also a method that defines the class. Similarly, the call form class ::
Example:
Class Student
{
Public $ stu_id;
Public $ stu_name;
Public static $ stu_count = 0;
Public function _ constuct ($ id, $ name)
{
$ This-> stu_id = $ id;
$ This-> stu_name = $ name;
Self: $ stu_count ++;
}
Public static function sayCount ()
{
Echo "run static ";
}
}
Student: sayCount ();
The main difference between static and non-static methods is whether an object's execution environment can be received.
Is it possible to assign a value to $ this in the method?
Only when an object calls a method can the object execution environment be passed to the method, and $ this can be assigned a value.
Non-static attributes cannot be used in static methods.
One static method can only process static data (static attributes)
Example:
Class Student
{
Public $ stu_id;
Public $ stu_name;
Public static $ stu_count = 0;
Public function _ constuct ($ id, $ name)
{
$ This-> stu_id = $ id;
$ This-> stu_name = $ name;
Self: $ stu_count ++;
}
Public static function sayCount ()
{
Echo self: $ stu_count;
}
}
Student: sayCount ();
Special method access problems in Php:
Emphasis: static methods should be called by class, non-static methods should be called by objects
Class C
{
Public function f1 ()
{
Echo 'F1
';
}
Public static function f2 ()
{
Echo 'F2
';
}
}
$ O = new C;
$ O-> f1 ();
C: f2 ();
However:
1. both static and non-static methods can be accessed using classes: However, if a class calls a non-static method statically, an error with nonstandard syntax is reported.
2. The difference lies in $ this.
You can use $ this in the method only when you call a non-static method using an object.
Class C
{
Public function f1 ()
{
Echo 'F1
';
}
Public static function f2 ()
{
Echo 'F2
';
}
}
C: f1 ();
C: f2 ();
Summary: the static and non-static methods of the class are saved in only one copy, but the runtime environment is different. You can use $ this in the method only when you call a non-static method using an object.