php class中self,parent,this的区别以及实例介绍_PHP
一,this
1,要用this,你必有是一个对像的形势,不然它会报错的,Fatal error: Using $this when not in object context。
2,this可以调用本类中的方法和属性,也可以调用父类中的可以调的方法和属性
二,self
1,self可以访问本类中的静态属性和静态方法,可以访问父类中的静态属性和静态方法。
2,用self时,可以不用实例化的
三,parent
1,parent可以访问父类中的静态属性和静态方法。
2,用parent时,可以不用实例化的
复制代码 代码如下:
class test{
public $public;
private $private;
protected $protected;
static $instance;
static $good = 'tankzhang
';
public $tank = 'zhangying
';
public function __construct(){
$this->public = 'public
';
$this->private = 'private
';
$this->protected = 'protected
';
}
public function tank(){ //私有方法不能继承,换成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
";
echo $this->public;
}
protected function pro_function(){
echo "you request protected function
";
echo $this->protected;
}
private function pri_function(){
echo "you request private function
";
echo $this->private;
}
static function sta_function(){
echo "you request static function
";
}
}
class test1 extends test{
static $love = "tank
";
private $aaaaaaa = "ying
";
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
";
}
}
error_reporting(E_ALL);
$test = new test1();
$test->tank(); //子类和父类有相同名字的属性和方法,实例化子类时,会调用子类中的方法。
test1::test1_function();
test1::extends_function(); //执行一部分后,报Fatal error: Using $this when not in object context in D:\xampp\htdocs\mytest\www4.php on line 32
?>
1,当我们调用$test->tank(); 这个方法时,tank里面的$this是一个对像 ,这个对像可以调用本类,父类中的方法和属性,
2,test1::test1_function(); 当我们用静态的方法去调用非静态方法时,会显示警告的,Non-static method test::test1_function() should not be called statically可以看出不,self可以调用本类,父类中的静态属性 ,parent可以调用父类中的静态属性 ,二者调用非静态属性会报错。代码中有注释
3,test1::extends_function(); 这一步会报错,报在非对像中使用$this 。为什么会这样呢,test1::extends_function();只是调用了class中的一个方法,并没有实例化,所以根本不存在什么对像,当父类中用到$this时,就会报错

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

Before introducing the usage of self in Python, let’s first introduce the classes and instances in Python. We know that the most important concepts of object-oriented are classes and instances. Classes are abstract templates, such as abstract things like students. , can be represented by a Student class. Instances are specific "objects" created based on classes. Each object inherits the same methods from the class, but its data may be different. 1. Take the Student class as an example. In Python, the class is defined as follows: classStudent(object):pass(Object) indicates which class the class inherits from. The Object class is all

Inobject-orientedprogramming,inheritanceallowsustocreatenewclassesthatinheritthepropertiesandmethodsofanexistingclass.Thispowerfulconceptenablescodereuse,modularity,andextensibilityinourprograms.Beforedivingintoaccessingparentclassattributes,let'shav

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

1. this keyword 1. Type of this: Which object is called is the reference type of that object 2. Usage summary 1. this.data;//Access attribute 2. this.func();//Access method 3.this( );//Call other constructors in this class 3. Explanation of usage 1.this.data is used in member methods. Let us see what will happen if this is not added classMyDate{publicintyear;publicintmonth;publicintday; publicvoidsetDate(intyear,intmonth,intday){ye

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

What is this? The following article will introduce you to this in JavaScript, and talk about the differences between this in different calling methods of functions. I hope it will be helpful to you!

The arrow function in JavaScript is a relatively new syntax. It does not have its own this keyword. On the contrary, the this of the arrow function points to the scope object containing it. The impacts are: 1. This in the arrow function is static; 2. Arrow Functions cannot be used as constructors; 3. Arrow functions cannot be used as methods.
