search
Homephp教程PHP源码php中this,parent,self关键字用法详解

this,parent,self都是在php中的class类中了,下面我来分别介绍this,parent,self在类中用应用与区别有需要的朋友可参考。

<script>ec(2);</script>

一,this

1,要用this,你必有是一个对像的形势,不然它会报错的,Fatal error: Using $this when not in object context。

2,this可以调用本类中的方法和属性,也可以调用父类中的可以调的方法和属性

 代码如下 复制代码

class UserName  
{   
//定义属性   
private $name;  
//定义构造函数  
function __construct( $name )  
{  
$this->name = $name;
//这里已经使用了this指针  
}  
//析构函数  
function __destruct(){}   
//打印用户名成员函数  
function printName()  
{  
print( $this->name );
//又使用了PHP关键字this指针  
}  
}   
//实例化对象  
$nameObject = new UserName
( "heiyeluren" );   
//执行打印  
$nameObject->printName();
 //输出: heiyeluren  
//第二次实例化对象  
$nameObject2 = new UserName( "PHP5" );  
//执行打印  
$nameObject2->printName(); //输出:PHP5  
?> 

我 们看,上面的类分别在11行和20行使用了this指针,那么当时this是指向谁呢?其实this是在实例化的时候来确定指向谁,比如第一次实例化对象 的时候(25行),那么当时this就是指向$nameObject对象,那么执行18行的打印的时候就把print( $this->name ),那么当然就输出了"heiyeluren"。


二,self

self是指向类本身,也就是PHP self关键字是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。
1,self可以访问本类中的静态属性和静态方法,可以访问父类中的静态属性和静态方法。
2,用self时,可以不用实例化的

 代码如下 复制代码

class Counter  
{  
//定义属性,包括一个静态变量  
private static $firstCount = 0;  
private $lastCount;  
//构造函数  
function __construct()  
{  
$this->lastCount = ++selft
::$firstCount;
 //使用PHP self关键字来调用静态变量,使用self
调用必须使用::(域运算符号)  
}  
//打印最次数值  
function printLastCount()  
{  
print( $this->lastCount );  
}   
}  
//实例化对象  
$countObject = new Counter();  
$countObject->printLastCount();
 //输出 1  
?> 

三,parent

parent:: 可用于调用父类中定义的成员方法。
parent::的追溯不仅于直接父类。
通过parent::调用父类方法

 代码如下 复制代码



class employee{
 protected  $sal=3000;  
 public function getSal(){
  $this->sal = $this->sal + 1200;  
  return $this->sal ;
 } 
}

class Manager extends employee {
 //如果想让经理在员工工资的基础上多发1500元.
 //必须先调用父类的getSal()方法.
 public function getSal(){  
  parent::getSal();// 这里调用了父类的方法.
  $this->sal = $this->sal + 1500;  
  return $this->sal ;
 } 
}
$emp = new employee();
echo "普通员工的工资是 " . $emp->getSal();
echo "
";

$manager = new Manager();
echo "经理的工资是: " . $manager->getSal();
?>

parent是指向父类的指针,一般我们使用parent来调用父类的构造函数。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.