Home > Article > Backend Development > Detailed explanation of some feature examples of PHP class
This article mainly introduces the characteristics of PHP classes, and analyzes the related usage skills of static methods and static member calls in PHP classes in the form of examples. Friends in need can refer to it
The examples in this article describe PHP Class characteristics. Share it with everyone for your reference, the details are as follows:
Object downward pass characteristics
When an object calls an instance method, and then goes to ## in the method #Static callIf you call a method of another class, the object in the source method (this) is obtained in the statically called method.
<?php class bee{ public $a = 1; public function f(){ echo $this->a; echo '<br>'; @lig::f(); } } class lig{ public $a = 2; public function f(){ echo $this->a; } } $obj = new bee(); $obj->f();The running result is: 1
1
function f(){ static $v = 1; }Static members in the class
class bee{ static $v = 1; static function f(){} }The dynamic in the method specifies the 'current class', which is different from self. static represents the class (dynamic) that calls this method, and self refers to its code The class where it is located (static)
class bee{ static public $a = 10; static public function f(){ echo get_class().':'; echo self::$a.'-'; echo static::$a; } } class lig extends bee{ static public $a = 20; } echo bee::f(); echo '<br>'; echo lig::f();The running result is as follows: bee:10-10
bee:10-20
Object-oriented Three major ideas
1. Encapsulation
is to encapsulate data and try not to show it to others. It can be considered that the most basic encapsulation is to encapsulate a lot of inside the data encapsulation class, but more strictly speaking, try to make theproperties private and provide operations to the outside through shared methods.
3. Polymorphism
usually refers to an object using The same method gets different resultsIt also means that different objects use the same method to get different results
The above is the detailed content of Detailed explanation of some feature examples of PHP class. For more information, please follow other related articles on the PHP Chinese website!