# ReflectionClass::newInstanceWithoutConstructor()

- Thread: https://externals.io/message/54924
- Original archive: https://news-web.php.net/php.internals/54924
- Started by Sebastian Bergmann on 2011-08-25T09:46:21+00:00
- Messages: 23
- 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

---

## #54924 — Sebastian Bergmann — 2011-08-25T09:46:21+00:00

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

I have attached a patch to https://bugs.php.net/bug.php?id=55490 that
  implements ReflectionClass::newInstanceWithoutConstructor() to create an
  object a class without invoking the constructor.

  As there are certain internal classes that would "crash" when their
  constructor is not invoked, this new method of the Reflection API only
  works for userland-defined classes.

  Unless there are objections, I will commit this change to PHP_5_4 and
  trunk on Monday. I will add the method to the documentation as well, of
  course.

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #54927 — Pierre Joye — 2011-08-25T10:20:43+00:00

URL: https://externals.io/message/54924#54927 · In reply to: #54924

hi,


>  I have attached a patch to https://bugs.php.net/bug.php?id=55490 that
>  implements ReflectionClass::newInstanceWithoutConstructor() to create an
>  object a class without invoking the constructor.
>
>  As there are certain internal classes that would "crash" when their
>  constructor is not invoked, this new method of the Reflection API only
>  works for userland-defined classes.

Looks good and as you do the check to avoid internal classes, that's all good :)

Cheers,
-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #54928 — Gustavo Lopes — 2011-08-25T11:46:39+00:00

URL: https://externals.io/message/54924#54928 · In reply to: #54924

On Thu, 25 Aug 2011 10:46:21 +0100, Sebastian Bergmann <sebastian@php.net>  
wrote:

>   I have attached a patch to https://bugs.php.net/bug.php?id=55490 that
>   implements ReflectionClass::newInstanceWithoutConstructor() to create  
> an
>   object a class without invoking the constructor.
>
>   As there are certain internal classes that would "crash" when their
>   constructor is not invoked, this new method of the Reflection API only
>   works for userland-defined classes.
>

What about userland classes that extend internal classes?

-- 
Gustavo Lopes

---

## #54929 — Patrick ALLAERT — 2011-08-25T12:25:09+00:00

URL: https://externals.io/message/54924#54929 · In reply to: #54924

2011/8/25 Sebastian Bergmann <sebastian@php.net>:
>  I have attached a patch to https://bugs.php.net/bug.php?id=55490 that
>  implements ReflectionClass::newInstanceWithoutConstructor() to create an
>  object a class without invoking the constructor.
>
>  As there are certain internal classes that would "crash" when their
>  constructor is not invoked, this new method of the Reflection API only
>  works for userland-defined classes.

+1 for the feature

>  Unless there are objections, I will commit this change to PHP_5_4

+0

> and trunk on Monday.

+1

> I will add the method to the documentation as well, of
>  course.

---

## #54930 — Richard Quadling — 2011-08-25T12:26:12+00:00

URL: https://externals.io/message/54924#54930 · In reply to: #54924

>  I have attached a patch to https://bugs.php.net/bug.php?id=55490 that
>  implements ReflectionClass::newInstanceWithoutConstructor() to create an
>  object a class without invoking the constructor.
>
>  As there are certain internal classes that would "crash" when their
>  constructor is not invoked, this new method of the Reflection API only
>  works for userland-defined classes.
>
>  Unless there are objections, I will commit this change to PHP_5_4 and
>  trunk on Monday. I will add the method to the documentation as well, of
>  course.

Wouldn't ReflectionClass::newMockInstance() be a better name?

What other use cases exists?



-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

---

## #54931 — Sebastian Bergmann — 2011-08-25T12:31:07+00:00

URL: https://externals.io/message/54924#54931 · In reply to: #54930

> Wouldn't ReflectionClass::newMockInstance() be a better name?

  It is not specific to stubbing/mocking.

> What other use cases exists?

  ORM and stubbing/mocking should be enough.

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #54932 — Etienne Kneuss — 2011-08-25T12:39:52+00:00

URL: https://externals.io/message/54924#54932 · In reply to: #54924

Hi,


>  I have attached a patch to https://bugs.php.net/bug.php?id=55490 that
>  implements ReflectionClass::newInstanceWithoutConstructor() to create an
>  object a class without invoking the constructor.

To me this feature makes no sense. But if people find use for it and
it remains in Reflection, I won't object to it, so +0.

>
>  As there are certain internal classes that would "crash" when their
>  constructor is not invoked, this new method of the Reflection API only
>  works for userland-defined classes.

I don't believe there should be this artificial distinction. Internal
classes should behave like userland ones in every aspect, let's not
put additional barriers.

If an internal class can't behave well without a constructor call,
that should already be fixed/prevented, as it's already possible by
extending it.

>
>  Unless there are objections, I will commit this change to PHP_5_4 and
>  trunk on Monday. I will add the method to the documentation as well, of
>  course.
>
> --
> Sebastian Bergmann                    Co-Founder and Principal Consultant
> http://sebastian-bergmann.de/                           http://thePHP.cc/
>
> --
> 
> 
>
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #54933 — Sebastian Bergmann — 2011-08-25T12:41:27+00:00

URL: https://externals.io/message/54924#54933 · In reply to: #54928

> What about userland classes that extend internal classes?

  Good point. How would I implement such a check?

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #54934 — Sebastian Bergmann — 2011-08-25T12:43:36+00:00

URL: https://externals.io/message/54924#54934 · In reply to: #54932

> To me this feature makes no sense. But if people find use for it and
> it remains in Reflection, I won't object to it, so +0.

  It should only be used for meta programming, of course ;-)

> If an internal class can't behave well without a constructor call,
> that should already be fixed/prevented, as it's already possible by
> extending it.

  I second that emotion but as long as those internal classes are not
  fixed I think it makes sense to disallow creating objects of internal
  classes without invoking their constructor.

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #54935 — Kalle Sommer Nielsen — 2011-08-25T12:47:06+00:00

URL: https://externals.io/message/54924#54935 · In reply to: #54933

Hi

2011/8/25 Sebastian Bergmann <sebastian@php.net>:
>  Good point. How would I implement such a check?

Speaking of which, wouldn't it be easier to check all our internal
classes for such failures and fix them accordingly with this addition
(Fix the dont-crash-without-calling-constructor)?

We could do like that of above and cast an exception instead of
 `E_RECOVERABLE_ERROR` (as I believe is being cast now) in 5.4.

-- 
regards,

Kalle Sommer Nielsen
kalle@php.net

---

## #54936 — Sebastian Bergmann — 2011-08-25T12:49:01+00:00

URL: https://externals.io/message/54924#54936 · In reply to: #54935

> Speaking of which, wouldn't it be easier to check all our internal
> classes for such failures and fix them accordingly with this addition
> (Fix the dont-crash-without-calling-constructor)?
>
> We could do like that of above and cast an exception instead of
> `E_RECOVERABLE_ERROR` (as I believe is being cast now) in 5.4.

  Makes sense to me.

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #54937 — Etienne Kneuss — 2011-08-25T12:49:03+00:00

URL: https://externals.io/message/54924#54937 · In reply to: #54934

Hi,


> 
> > […]
>
>  It should only be used for meta programming, of course ;-)
>
> > […]
>
>  I second that emotion but as long as those internal classes are not
>  fixed I think it makes sense to disallow creating objects of internal
>  classes without invoking their constructor.

It is already possible to do that currently, with or without this
feature, so you're not preventing much abuse by doing that here. As I
said, artificial distinction.

This feature however alters the necessary checks for missing
constructor calls (at least the ones implemented in SPL as of 5.4), as
those assume a userland subclass.

>
> --
> Sebastian Bergmann                    Co-Founder and Principal Consultant
> http://sebastian-bergmann.de/                           http://thePHP.cc/
>
> --
> 
> 
>
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #54938 — Pierre Joye — 2011-08-25T12:49:24+00:00

URL: https://externals.io/message/54924#54938 · In reply to: #54933

> 
> > […]
>
>  Good point. How would I implement such a check?

Checking the same thing than what instanceof does


Cheers,
-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #54939 — Etienne Kneuss — 2011-08-25T12:54:17+00:00

URL: https://externals.io/message/54924#54939 · In reply to: #54936

Hi,


> 
> > […]
>
>  Makes sense to me.

There is already some standardized checks for 5.4 that wrap around the
get_constructor handler. But it should/could be standardized at the
engine level. And those raise exceptions in case they detect a
uncalled constructor.

>
> --
> Sebastian Bergmann                    Co-Founder and Principal Consultant
> http://sebastian-bergmann.de/                           http://thePHP.cc/
>
> --
> 
> 
>
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #54940 — Etienne Kneuss — 2011-08-25T12:54:38+00:00

URL: https://externals.io/message/54924#54940 · In reply to: #54939

> Hi,
>
> 
> > […]
>
> There is already some standardized checks for 5.4 that wrap around the
> get_constructor handler.

In SPL, that is

> But it should/could be standardized at the
> engine level. And those raise exceptions in case they detect a
> uncalled constructor.
>
> > […]
>
>
>
> --
> Etienne Kneuss
> http://www.colder.ch
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #54941 — Gustavo Lopes — 2011-08-25T12:54:38+00:00

URL: https://externals.io/message/54924#54941 · In reply to: #54933

On Thu, 25 Aug 2011 13:41:27 +0100, Sebastian Bergmann <sebastian@php.net>  
wrote:

> 
> > […]
>
>   Good point. How would I implement such a check?
>

You could fetch the parent and do the same check, and so on. I think you  
might be able to just check whether ce.create_object is not NULL, because  
if the internal class doesn't replace create_object, there's no chance of  
that class of crashes occurring; plus, they're propagated to the  
subclasses.


-- 
Gustavo Lopes

---

## #54942 — Sebastian Bergmann — 2011-08-25T13:27:33+00:00

URL: https://externals.io/message/54924#54942 · In reply to: #54941

> You could fetch the parent and do the same check, and so on. I think you
> might be able to just check whether ce.create_object is not NULL, because
> if the internal class doesn't replace create_object, there's no chance of
> that class of crashes occurring; plus, they're propagated to the subclasses.

  I updated the patch accordingly. Thanks!

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #54944 — Benjamin Eberlei — 2011-08-25T14:03:34+00:00

URL: https://externals.io/message/54924#54944 · In reply to: #54942

Thanks for working on this Sebastian, its really appreciated! +1 from me!

On Thu, Aug 25, 2011 at 3:27 PM, Sebastian Bergmann <sebastian@php.net>wrote:

> 
>
> > […]
>
>  I updated the patch accordingly. Thanks!
>
>
> --
> Sebastian Bergmann                    Co-Founder and Principal Consultant
> http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #54945 — guilhermeblanco@gmail.com — 2011-08-25T15:12:51+00:00

URL: https://externals.io/message/54924#54945 · In reply to: #54944

Sebastian,

This is a hidden  gem for us! =D
Thanks a lot for putting your effort into this.

Cheers,


> Thanks for working on this Sebastian, its really appreciated! +1 from me!
>
> On Thu, Aug 25, 2011 at 3:27 PM, Sebastian Bergmann <sebastian@php.net>wrote:
>
> > […]
>



-- 
Guilherme Blanco
Mobile: +55 (11) 8118-4422
MSN: guilhermeblanco@hotmail.com
São Paulo - SP/Brazil

---

## #54947 — Patrick ALLAERT — 2011-08-25T16:16:45+00:00

URL: https://externals.io/message/54924#54947 · In reply to: #54941

2011/8/25 Gustavo Lopes <glopes@nebm.ist.utl.pt>:
> You could fetch the parent and do the same check, and so on.

Checking only the topmost should be enough if traversing the whole
inheritance could be avoided.

> I think you
> might be able to just check whether ce.create_object is not NULL, because if
> the internal class doesn't replace create_object, there's no chance of that
> class of crashes occurring; plus, they're propagated to the subclasses.
>
>
> --
> Gustavo Lopes

---

## #54948 — Patrick ALLAERT — 2011-08-25T16:18:40+00:00

URL: https://externals.io/message/54924#54948 · In reply to: #54947

2011/8/25 Patrick ALLAERT <patrickallaert@php.net>:
> 2011/8/25 Gustavo Lopes <glopes@nebm.ist.utl.pt>:
> > […]
>
> Checking only the topmost should be enough if traversing the whole
> inheritance could be avoided.

Well, of course I highly second the opinion that not making
distinction and fixing possible bugs in internal classes is a much
better approach.

---

## #54968 — Ferenc Kovacs — 2011-08-26T07:51:05+00:00

URL: https://externals.io/message/54924#54968 · In reply to: #54924

>  I have attached a patch to https://bugs.php.net/bug.php?id=55490 that
>  implements ReflectionClass::newInstanceWithoutConstructor() to create an
>  object a class without invoking the constructor.
>
>  As there are certain internal classes that would "crash" when their
>  constructor is not invoked, this new method of the Reflection API only
>  works for userland-defined classes.
>
>  Unless there are objections, I will commit this change to PHP_5_4 and
>  trunk on Monday. I will add the method to the documentation as well, of
>  course.
>
> --
> Sebastian Bergmann                    Co-Founder and Principal Consultant
> http://sebastian-bergmann.de/                           http://thePHP.cc/
>
> --
> 
> 
>
>

hi Sebastian,

would this method give the same result as
unserialize("O:8:\"stdClass\":0:{}")  for example?
I used this trick in the past when I needed to instantiate a class
without calling it's constructor.
of course having a dedicated method for this is much more convenient

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #54971 — Sebastian Bergmann — 2011-08-26T10:01:32+00:00

URL: https://externals.io/message/54924#54971 · In reply to: #54968

Am 26.08.2011 09:51, schrieb Ferenc Kovacs:
> would this method give the same result as
> unserialize("O:8:\"stdClass\":0:{}")  for example?

 That is the whole point of it, yes.

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/
