One, this
1, to use this, you have to be a situation of the image, otherwise it will be an error, Fatal error:using $this when not in the object context.
2,this can call methods and properties in this class, or you can call methods and properties that are adjustable in the parent class
Two, self
1,self can access static and static methods in this class, and you can access static properties and static methods in the parent class.
2, when you use self, you can not instantiate the
Third, parent
1,parent can access static properties and static methods in the parent class.
2, when using parent, you can not instantiate the
Copy Code code as follows:
<?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 method cannot be inherited, replace with public,protected
if (!isset (self:: $instance [Get_class ()])
{
$c = Get_class ();
Self:: $instance = new $c;
}
Return self:: $instance;
}
Public Function pub_function () {
echo "You are 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::p ro_function ();
echo "You request Extends_private function<br>";
}
}
Error_reporting (E_all);
$test = new Test1 ();
$test->tank (); Subclasses and the parent class have properties and methods of the same name, and when instantiating a subclass, the method in the child class is invoked.
Test1::test1_function ();
Test1::extends_function (); After part of the execution, the newspaper fatal error:using $this when isn't in the object context into D:\xampp\htdocs\mytest\www4.php on line 32
?>
1, when we call $test->tank (); In this method, the tank inside the $this is a pair of like, which can call the class, the method and the property in the parent class,
2,test1::test1_function (); When we use static methods to invoke Non-static methods, a warning is displayed, Non-static method Test::test1_function () should the not to called statically can see that no, self can invoke this class, Static properties in the parent class, where parent can invoke static properties in the parent class, and the two Non-static properties call the error. There are comments in the code
3,test1::extends_function (); This step will be an error, reported in the image of the use of $this. Why So, test1::extends_function (); Just call one of the methods in class, and there is no instantiation, so there is no image at all, and when $this is used in the parent class, an error is made