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

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

问题描述

我一直在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.

  • Reference: http://www.php.net/manual/en/language.oop5.basic.php
  • Primer: http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html

示例:

<?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.

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

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