PHP Object-oriented? Static members
Static properties
Properties declared with the static keyword
The static property, logically, is the attribute defined above the class. guarantees a class that corresponds to a property.
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;
}
}
To access static properties:
Access by class: At the use of static accesses 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 + +;
}
}
:: Access is called static access,-> for non-static access (object access)
When accessing a static property, if it is accessed within a class: You can use the Self current 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 + +;
}
}
Note: The difference between $this and self
$this represents this object, and self indicates that the class
Static methods
The logical meaning of the static method is also the method of defining the re-class. Similarly, 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 you can receive an object's execution environment.
Is whether the $this within the method can be assigned
Only when an object invokes a method can the object execution environment be passed into a method, $this is assigned a value.
Non-static properties cannot be used in static methods.
A static method that can handle only static data (static properties)
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 Issues in PHP:
Emphasis: Static methods should be called by the class, and non-static methods should be called by the object
Class C
{
Public Function F1 ()
{
Echo ' F1
’;
}
public static function F2 ()
{
Echo ' F2
’;
}
}
$o = new C;
$o->f1 ();
C::f2 ();
But:
1. Class access is available for both static and non-static methods: However, if a class statically invokes a non-static method, an error with a nonstandard syntax is reported.
2. The difference lies in the $this
You can use $this within a method only if you use the object to invoke a non-static method.
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 only one copy of the class, but the runtime environment is different. You can use $this within a method only if you use the object to invoke a non-static method.