Home  >  Article  >  Backend Development  >  Detailed explanation of some feature examples of PHP class

Detailed explanation of some feature examples of PHP class

伊谢尔伦
伊谢尔伦Original
2017-06-29 13:25:581090browse

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 &#39;<br>&#39;;
    @lig::f();
  }
}
class lig{
  public $a = 2;
  public function f(){
    echo $this->a;
  }
}
$obj = new bee();
$obj->f();

The running result is:

1

1

static Late static binding

static has 3 different meanings of syntax

Static variables in functions

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().&#39;:&#39;;
    echo self::$a.&#39;-&#39;;
    echo static::$a;
  }
}
class lig extends bee{
  static public $a = 20;
}
echo bee::f();
echo &#39;<br>&#39;;
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 the

properties private and provide operations to the outside through shared methods.

2. Inheritance

3. Polymorphism

usually refers to an object using The same method gets different results

It 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn