Home >Backend Development >PHP Tutorial >::, ->, self, $this operator in PHP_PHP tutorial

::, ->, self, $this operator in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:09815browse

When accessing member variables or methods in a PHP class, if the referenced variable or method is declared as const (defining constant) or static (declaring static), then you must use the operator::, otherwise if it is referenced The variable or method is not declared as const or static, then the operator -> must be used.

In addition, if you access a const or static variable or method from within the class, you must use self-reference. On the contrary, if you access a non-const or static variable or method from within the class, you must use self-reference. $this.

$this instance

The code is as follows Copy code
 代码如下 复制代码

// this是指向当前对象的指针

class test_this{
private $content; //定义变量

function __construct($content){ //定义构造函数
$this->content= $content;
    }
    function __destruct(){}//定义析构函数
   
    function printContent(){//定义打印函数
        echo $this->content.'
';
    }
}

$test=new test_this('北京欢迎你!'); //实例化对象
$test->printContent();//北京欢迎你!

// this is a pointer to the current object
代码如下 复制代码

//parent是指向父类的指针

class test_parent{ //基类
public $name; //定义姓名 父类成员需要定义为public,才能够在继承类中直接使用 this来调用。
function __construct($name){
$this->name=$name;
    }
}
class test_son extends test_parent{ //派生类  继承test_parent
    public $gender;//定义性别
    public $age;    //定义年龄
    function __construct($gender,$age){ //继承类的构造函数
        parent::__construct('nostop');//使用parent调用父类的构造函数,来进行对父类的实例化
        $this->gender=$gender;
        $this->age=$age;
    }
    function __destruct(){}
    function print_info(){
        echo $this->name.'是个'.$this->gender.',今年'.$this->age.'岁'.'
';
    }
}

$nostop=new test_son('女性','22');//实例化test_son对象
$nostop->print_info();//执行输出函数  nostop是个女性,今年23岁

class test_this{
private $content; //Define variables

Function __construct($content){ //Define constructor
             $this->content= $content;
}
Function __destruct(){}//Define destructor

Function printContent(){//Define printing function
echo $this->content.'
';
}
} $test=new test_this('Welcome to Beijing!'); //Instantiated object
$test->printContent();//Welcome to Beijing! ::Usage
The code is as follows Copy code
//parent is a pointer to the parent class class test_parent{ //base class
Public $name; //Define name Parent class members need to be defined as public before they can be called directly using this in the inherited class.
Function __construct($name){
            $this->name=$name;
}
}
class test_son extends test_parent{ // Derived class inherits test_parent
Public $gender;//Define gender
Public $age; //Define age
Function __construct($gender,$age){ //Inherit the constructor of the class
parent::__construct('nostop');//Use parent to call the constructor of the parent class to instantiate the parent class
          $this->gender=$gender;
           $this->age=$age;
}
Function __destruct(){}
Function print_info(){
echo $this->name.'is a'.$this->gender.', this year'.$this->age.'years old'.'
';
}
} $nostop=new test_son('female','22');//Instantiate the test_son object
$nostop->print_info();//Execute the output function nostop is a female, 23 years old this year


Use the form self::$name. Note that the declaration format of the const attribute is const PI=3.14, not const $PI=3.14

​         private static $name="static class_a";
The code is as follows
 代码如下 复制代码

class clss_a {
    
     private static  $name="static class_a";
    
     const PI=3.14;
     public $value;   
        
     public static function getName()
     {
        return self::$name;   
     }
     //这种写法有误,静态方法不能访问非静态属性
     public static function getName2()
     {
         return self::$value;
     }
     public function getPI()
     {
       return self::PI;   
     }
    
    
 }

Copy code


class clss_a {
​   const PI=3.14;

Public $value;

                                    {            return self::$name;                                                } //This way of writing is wrong, static methods cannot access non-static properties Public static function getName2() {            return self::$value; } Public function getPI() {          return self::PI;                                      } ​  
​   }
Another point to note is that if a class method is static, the properties it accesses must also be static. When the internal method of a class accesses attributes that are not declared const or static, use the form $this->value ='class_a';. http://www.bkjia.com/PHPjc/628859.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628859.htmlTechArticleWhen accessing member variables or methods in a PHP class, if the referenced variable or method is declared as const ( Define constant) or static (declare static), then you must use the operator:...
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