$this 在 PHP 中意味着什么?
Possible Duplicate:
PHP: self vs this
Hello,
Could you help me understanding the meaning of the PHP variable name $this
?
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$this
指的是您所在的班级。例如
希望这会有所帮助。
$this
refers to the class you are in.For example
Hope this helps.
您可能想看看 在 PHP5 中,使用 self 和 $this 之间有什么区别?何时合适?
基本上,
$this
指的是当前对象。You might want to have a look at the answers in In PHP5, what is the difference between using self and $this? When is each appropriate?
Basically,
$this
refers to the current object.$this
是在对象内使用的受保护变量,$this
允许您在内部访问类文件。示例
它基本上是一个变量范围问题,
$this
只允许在已启动的对象内,并且仅引用该对象及其父对象,您可以运行私有方法并设置私有变量,如外部你不能的范围。self
关键字也非常相似,除了它指的是 class 中的静态方法,static 基本上意味着你不能使用$this
因为它不是还没有一个对象,您必须使用 self::setAge(); 并且如果该setAge
方法被声明为静态,那么您不能从该对象的瞬间调用它/object
一些供您入门的链接:
$this
is a protected variable that's used within a object,$this
allows you to access a class file internally.Example
Its basically a variable scope issue,
$this
is only allowed within a object that has been initiated and refers to that object and its parents only, you can run private methods and set private variables where as out side the scope you cannot.also the
self
keyword is very similar apart from it refers to static methods within class, static basically means that you cant use$this
as its not an object yet, you must useself::setAge();
and if thatsetAge
method is declared static then you cannot call it from an instant of that object /object
Some links for you to get started: