如何以最佳和简单的方式解释"this"关键字? [英] How to explain 'this' keyword in a best and simple way?

查看:64
本文介绍了如何以最佳和简单的方式解释"this"关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用'this'关键字已有很长时间了.但是,当有人要求我解释它时,我很困惑如何解释它.我知道我可以在类的方法中使用它来访问同一类的任何变量和方法.

I am using 'this' keyword for a long time. But when someone asks me to explain it, I am confused that how to explain it. I know that I can use this in a method of class to access any variable and method of the same class.

    class MyClass{

      function MyMethod1(){
        echo "Hello World";
      }

      function MyMethod2(){
        $this->MyMethod1();
      }

    }

它是否是我们不需要初始化的类的对象,并且只能在该类或其他任何对象中使用.如何解释?

Is it a object of a class that we don't need to initialise and can be used only within the class or anything else. How to explain?

谢谢

推荐答案

类是对象的模子:它指定对象的外观(变量)和功能(功能).

A class is a mold for an object: it specifies how the object looks like (variables) and what it can do (functions).

如果实例化一个类:创建一个对象.如果创建类,则可以使用"this"来引用对象本身.这就是为什么您不能设置"this"的原因,因为它与对象有关.这是一个特殊的只读变量.

If you instanciate a class: you create an object. If you create the class, you can use "this" to refer to the object itsself. This is why you can't set the "this", because it's related to the object. It's a special, read-only variable.

这篇关于如何以最佳和简单的方式解释"this"关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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