When to use self over $this?

2018-12-31 01:49发布

In PHP 5, what is the difference between using self and $this?

When is each appropriate?

标签: php class scope
22条回答
有味是清欢
2楼-- · 2018-12-31 02:51

Additionally since $this:: has not been discussed yet.

For informational purposes only, as of PHP 5.3 when dealing with instantiated objects to get the current scope value, as opposed to using static::, one can alternatively use $this:: like so.

http://ideone.com/7etRHy

class Foo
{
    const NAME = 'Foo';

    //Always Foo::NAME (Foo) due to self
    protected static $staticName = self::NAME;

    public function __construct()
    {
        echo $this::NAME;
    }

    public function getStaticName()
    {
       echo $this::$staticName;
    }
}

class Bar extends Foo
{
    const NAME = 'FooBar';

    /**
     * override getStaticName to output Bar::NAME
     */
    public function getStaticName()
    {
        $this::$staticName = $this::NAME;
        parent::getStaticName();
    }
}

$foo = new Foo; //outputs Foo
$bar = new Bar; //outputs FooBar
$foo->getStaticName(); //outputs Foo
$bar->getStaticName(); //outputs FooBar
$foo->getStaticName(); //outputs FooBar

Using the code above is not common or recommended practice, but is simply to illustrate its usage, and is to act as more of a "Did you know?" in reference to the original poster's question.

It also represents the usage of $object::CONSTANT for example echo $foo::NAME; as opposed to $this::NAME;

查看更多
不流泪的眼
3楼-- · 2018-12-31 02:52

Inside a class definition, $this refers to the current object, while self refers to the current class.

It is necessary to refer to a class element using self, and refer to an object element using $this.

self::STAT // refer to a constant value
self::$stat // static variable
$this->stat // refer to an object variable  
查看更多
梦该遗忘
4楼-- · 2018-12-31 02:52
  • The object pointer $this to refers to the current object.
  • The class value "static" refers to the current object.
  • The class value "self" refers to the exact class it was defined in.
  • The class value "parent" refers to the parent of the exact class it was defined in.

See the following example which shows overloading.

<?php

class A {

    public static function newStaticClass()
    {
        return new static;
    }

    public static function newSelfClass()
    {
        return new self;
    }

    public function newThisClass()
    {
        return new $this;
    }
}

class B extends A
{
    public function newParentClass()
    {
        return new parent;
    }
}


$b = new B;

var_dump($b::newStaticClass()); // B
var_dump($b::newSelfClass()); // A because self belongs to "A"
var_dump($b->newThisClass()); // B
var_dump($b->newParentClass()); // A


class C extends B
{
    public static function newSelfClass()
    {
        return new self;
    }
}


$c = new C;

var_dump($c::newStaticClass()); // C
var_dump($c::newSelfClass()); // C because self now points to "C" class
var_dump($c->newThisClass()); // C
var_dump($b->newParentClass()); // A because parent was defined *way back* in class "B"

Most of the time you want to refer to the current class which is why you use static or $this. However, there are times when you need self because you want the original class regardless of what extends it. (Very, Very seldom)

查看更多
浪荡孟婆
5楼-- · 2018-12-31 02:53

According to http://www.php.net/manual/en/language.oop5.static.php there is no $self. There is only $this, for referring to the current instance of the class (the object), and self, which can be used to refer to static members of a class. The difference between an object instance and a class comes into play here.

查看更多
登录 后发表回答