Differences between self parent this in php class and examples

  • 2020-05-30 19:46:15
  • OfStack

1, this

1, to use this, you must have an image of the situation, or it will report an error, Fatal error: Using $this when not in object context.
2. this can call methods and properties in this class as well as tunable methods and properties in its parent class

2, self

1. self can access the static properties and static methods in this class and the static properties and static methods in the parent class.
2. You can use self without instantiating it


3, parent

1, parent can access static properties and static methods in the parent class.
2. You can use parent without instantiating it


<?php
class test{
 public $public;
 private $private;
 protected $protected;
 static $instance;
 static $good = 'tankzhang <br>';
 public $tank = 'zhangying <br>';
 public  function __construct(){
 $this->public    = 'public     <br>';
 $this->private   = 'private    <br>';
 $this->protected = 'protected  <br>';
 }
 public function tank(){                          // Private methods cannot be inherited, instead public,protected
 if (!isset(self::$instance[get_class()]))
 {
 $c = get_class();
 self::$instance = new $c;
 }
 return self::$instance;
 }    
 public function pub_function() {
 echo "you request public function<br>";
 echo $this->public;
 }
 protected  function pro_function(){
 echo "you request protected function<br>";
 echo $this->protected;
 }
 private function pri_function(){
 echo "you request private function<br>";
 echo $this->private;
 }
 static function sta_function(){
 echo "you request static function<br>";
 }
}
class test1 extends test{
 static $love = "tank <br>";
 private $aaaaaaa = "ying <br>";
 public function __construct(){
 parent::tank();
 parent::__construct();
 }
 public function tank(){
 echo $this->public;
 echo $this->protected;
 echo $this->aaaaaaa;
 $this->pro_function();
 }
 public  function test1_function(){
 echo self::$love;
 echo self::$good;
 echo parent::$good;
 echo parent::$tank;   //Fatal error: Access to undeclared static property: test::$tank
 echo self::$tank;     //Fatal error: Access to undeclared static property: test::$tank
 }
 static function extends_function(){
 parent::sta_function();
 self::pro_function();
 echo "you request extends_private function<br>";
 }
}
error_reporting(E_ALL);
$test = new test1();
$test->tank();            // Subclasses and superclasses have properties and methods of the same name, and methods in subclasses are called when subclasses are instantiated. 
test1::test1_function();
test1::extends_function();  // perform 1 After the part, report Fatal error: Using $this when not in object context in D:\xampp\htdocs\mytest\www4.php on line 32
?>

1, when we call $test- > tank (); In this method, $this in tank is an image, and this image can call the methods and properties in this class, the parent class,

2, test1: : test1_function (); Non-static method test::test1_function() should not be called statically you can see that no, self can call this class, the static property in the parent class, parent can call the static property in the parent class, and 2 can call the non-static property and get an error. There are comments in the code

3, test1: : extends_function (); This step will report an error using $this in the non-counter. Why is that? test1::extends_function(); Only one of the methods in class is called, and there is no instantiation, so there is no such thing as an error when $this is used in the parent class. Right


Related articles: