# getInstance and new self()

- Thread: https://externals.io/message/8452
- Original archive: https://news-web.php.net/php.internals/8452
- Started by Jeffrey Moss on 2004-03-09T00:03:18+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #8452 — Jeffrey Moss — 2004-03-09T00:03:18+00:00

URL: https://externals.io/message/8452#8452

I found this on a zend engine 2 list archive, but didn't see an answer, has
it been resolved? I am trying to create a singleton class heirarchy without
having to override getInstance....

<?php
  class Test {
    public static function getInstance() {
      return new self();
    }
  }

  class Foo extends Test {
  }

  var_dump(Foo::getInstance());
?>
thekid@friebes:~/devel/php/tests > php5 self.php
object(test)#1 (0) {
}

Expected output would be "object(foo)", so I guess either "self" should
not be evaluated at compile time or this should be disallowed. I'd
prefer the first:)

http://www.zend.com/lists/engine2/200307/msg00033.html

---

## #8513 — Andi Gutmans — 2004-03-14T17:31:33+00:00

URL: https://externals.io/message/8452#8513 · In reply to: #8452

self:: is always evaluated at compile-time and refers to the class it is 
defined in and not the concrete object/class.

Andi

At 05:03 PM 3/8/2004 -0700, Jeffrey Moss wrote:
>I found this on a zend engine 2 list archive, but didn't see an answer, has
>it been resolved? I am trying to create a singleton class heirarchy without
>having to override getInstance....
>
><?php
>   class Test {
>     public static function getInstance() {
>       return new self();
>     }
>   }
>
>   class Foo extends Test {
>   }
>
>   var_dump(Foo::getInstance());
>?>
>thekid@friebes:~/devel/php/tests > php5 self.php
>object(test)#1 (0) {
>}
>
>Expected output would be "object(foo)", so I guess either "self" should
>not be evaluated at compile time or this should be disallowed. I'd
>prefer the first:)
>
>http://www.zend.com/lists/engine2/200307/msg00033.html

---

## #8515 — Ferdinand Beyer — 2004-03-14T20:02:27+00:00

URL: https://externals.io/message/8452#8515 · In reply to: #8452

> self:: is always evaluated at compile-time and refers to the class it is 
> defined in and not the concrete object/class.
> 
> Andi

I think it would be a better for self to refer to the concrete class. 
Otherwise it is not possible to access overridden static functions or 
to instantiate the concrete class.

-- 
Ferdinand Beyer
<fb@fbeyer.com
