# how to instantiate new object

- Thread: https://externals.io/message/60409
- Original archive: https://news-web.php.net/php.internals/60409
- Started by Yader Hernandez on 2012-05-02T03:46:20+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #60409 — Yader Hernandez — 2012-05-02T03:46:20+00:00

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

Hello,

I was wondering how to create a new object from a function call?

By this I mean if I call foo() from a script, that should return a new
instance of an object. If I call foo() again, it should be a new instance
of an object as well.

I haven't been able to find any good examples from the code I've been
reading.

Does anyone know of a good place to find an example?

thanks

---

## #60410 — Yader Hernandez — 2012-05-02T05:16:33+00:00

URL: https://externals.io/message/60409#60410 · In reply to: #60409

On Tue, May 1, 2012 at 8:46 PM, Yader Hernandez
<yader.hernandez@gmail.com>wrote:

> Hello,
>
> I was wondering how to create a new object from a function call?
>
> By this I mean if I call foo() from a script, that should return a new
> instance of an object. If I call foo() again, it should be a new instance
> of an object as well.
>
> I haven't been able to find any good examples from the code I've been
> reading.
>
> Does anyone know of a good place to find an example?
>
> thanks
>

To be a little clearer I mean that in my extension, I can call my custom
object. So any calls from a script will be able to do $o = new Foo(). I
registered my struct in zend_objects_store_put, so I understand how the
instantiate is happening.

But now I'm implementing a ZEND_METHOD that requires a new instance of Foo
and I've been stuck for a few days trying to find a nice clean example.

I was wondering if someone knew of a good place look into?

thanks

---

## #60411 — Laruence — 2012-05-02T05:27:43+00:00

URL: https://externals.io/message/60409#60411 · In reply to: #60410

On Wed, May 2, 2012 at 1:16 PM, Yader Hernandez
<yader.hernandez@gmail.com> wrote:
> On Tue, May 1, 2012 at 8:46 PM, Yader Hernandez
> <yader.hernandez@gmail.com>wrote:
>
> > […]
Hi:

    MAKE_STD_ZVAL(instance);
    object_init(instance , class_entry_ce);

    and if you want to call the contructor,  you have to call it explicitly.

   see: http://lxr.php.net/opengrok/xref/PECL/yaf/views/simple.c#216

thanks


> > […]
>
> To be a little clearer I mean that in my extension, I can call my custom
> object. So any calls from a script will be able to do $o = new Foo(). I
> registered my struct in zend_objects_store_put, so I understand how the
> instantiate is happening.
>
> But now I'm implementing a ZEND_METHOD that requires a new instance of Foo
> and I've been stuck for a few days trying to find a nice clean example.
>
> I was wondering if someone knew of a good place look into?
>
> thanks



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #60412 — Laruence — 2012-05-02T05:28:32+00:00

URL: https://externals.io/message/60409#60412 · In reply to: #60411

> On Wed, May 2, 2012 at 1:16 PM, Yader Hernandez
> <yader.hernandez@gmail.com> wrote:
> > […]
> Hi:
>
>    MAKE_STD_ZVAL(instance);
>    object_init(instance , class_entry_ce);
typo, should be object_init_ex.

thanks
>
>    and if you want to call the contructor,  you have to call it explicitly.
>
>   see: http://lxr.php.net/opengrok/xref/PECL/yaf/views/simple.c#216
>
> thanks
>
>
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #60413 — Laruence — 2012-05-02T05:29:32+00:00

URL: https://externals.io/message/60409#60413 · In reply to: #60412

BTW:  you should write to pecl-dev ML for such questions.

thanks


> 
> > […]
> typo, should be object_init_ex.
>
> thanks
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #60416 — johannes@schlueters.de — 2012-05-02T09:30:52+00:00

URL: https://externals.io/message/60409#60416 · In reply to: #60411

> On Wed, May 2, 2012 at 1:16 PM, Yader Hernandez
> <yader.hernandez@gmail.com> wrote:
> > […]
> Hi:
> 
>     MAKE_STD_ZVAL(instance);
>     object_init(instance , class_entry_ce);
> 
>     and if you want to call the contructor,  you have to call it explicitly.
> 
>    see: http://lxr.php.net/opengrok/xref/PECL/yaf/views/simple.c#216

This doesn't seem to run the ctor. Full example is
ZEND_METHOD(reflection_class, newInstance)
http://lxr.php.net/opengrok/xref/PHP_5_4/ext/reflection/php_reflection.c#4092

johannes
