1024programmer PHP In PHP::, ->, self, $this operators

In PHP::, ->, self, $this operators

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

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

$this instance

The code is as follows
<?php

// this is a pointer to the current object

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!

::Instructions

The code is as follows
//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 is ‘.$this->age.’.’
‘;
}
}

$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

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;                                    
}
//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 thing to note is that if a class method is static, the properties it accesses must also be static.
The internal method access of the class is not declared as const and static

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/in-php-self-this-operators/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索