PHP 中的 $this 变量是什么意思? [英] What does the variable $this mean in PHP?

查看:33
本文介绍了PHP 中的 $this 变量是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 PHP 中看到变量 $this,但我不知道它的用途.我从来没有亲自使用过.

I see the variable $this in PHP all the time and I have no idea what it's used for. I've never personally used it.

谁能告诉我变量 $this 在 PHP 中是如何工作的?

Can someone tell me how the variable $this works in PHP?

推荐答案

它是对当前对象的引用,在面向对象的代码中最常用.

It's a reference to the current object, it's most commonly used in object oriented code.

示例:

<?php
class Person {
    public $name;

    function __construct( $name ) {
        $this->name = $name;
    }
};

$jack = new Person('Jack');
echo $jack->name;

这将Jack"字符串存储为所创建对象的属性.

This stores the 'Jack' string as a property of the object created.

这篇关于PHP 中的 $this 变量是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆