Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » PHP
  • » [PHP-DEV] Follow the object constructor chain ... options [RSS Feed]

#1 Oct. 29, 2005 03:35:49

Bob S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Follow the object constructor chain ... options


What would be the suggested method of simulating parent::__construct in
C-level objects?

I looked through the source and could find no examples to draw from.



Since my base object is the foundation of my extension, its constructor has
to be called on each derived object creation.



I have a few ideas that have to do with looping through ce->parent and
zend_call_function.

Was wondering if you guys can offer some of your experience on how you would
approach this problem.



Bob Silva

Offline

#2 Oct. 29, 2005 08:19:07

Bob S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Follow the object constructor chain ... options


I believe I have it working. Anyone see any blatant problems with this?
Also, would it make sense to have this functionality in the engine itself?
Seeing as that internal classes have inheritance capabilities, it seems
logical to have the functionality to call a parent constructor within the
engine itself. I could definitely see this being useful within the SPL as
its class library continues to grow as well.

void pow_call_parent_constructor(zval *object TSRMLS_DC)
{
zend_class_entry *ce = EG(function_state_ptr)->function->common.scope;

if (ce->parent && ce->parent->constructor) {
zval *retval_ptr;
zend_fcall_info fci;
zend_fcall_info_cache fcc;

if (!(ce->parent->constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
return;
}

fci.size = sizeof(fci);
fci.function_table = NULL;
fci.function_name = NULL;
fci.symbol_table = NULL;
fci.object_pp = &object;
fci.retval_ptr_ptr = &retval_ptr;
fci.param_count = 0;
fci.params = NULL;
fci.no_separation = 1;

fcc.initialized = 1;
fcc.function_handler = ce->parent->constructor;
fcc.calling_scope = ce;
fcc.object_pp = &object;

if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
return;
}
if (retval_ptr) {
zval_ptr_dtor(&retval_ptr);
}
}
}

Forgive me if its crude, but it appears to work.

Bob


-----Original Message-----
From: Bob Silva
Sent: Friday, October 28, 2005 7:35 PM
To: intern***@*ists.php.net
Subject: Follow the object constructor chain ... options

What would be the suggested method of simulating parent::__construct in
C-level objects?

I looked through the source and could find no examples to draw from.



Since my base object is the foundation of my extension, its constructor has
to be called on each derived object creation.



I have a few ideas that have to do with looping through ce->parent and
zend_call_function.

Was wondering if you guys can offer some of your experience on how you would
approach this problem.



Bob Silva

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#3 Oct. 29, 2005 11:58:41

Marcus B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Follow the object constructor chain ... options


Hello Bob,

why so complex and potential wrong?

zend_class_entry *ce = Z_OBJ_CE(object);
zend_function *ctor = ce->parent ? ce->parent->constructor : NULL;

if (ctor && ctor->common.fn_flags & ZEND_ACC_PUBLIC) {
zend_call_method_with_0_params(object, ce, ctor, "__construct", NULL);
}

btw, why public? the parent ctor must only be called from a derived
class instance, thus you want to check against !private. But in that
case either the class or the ctor should be final and thus you shouldn't
have this issue.

marcus

Saturday, October 29, 2005, 9:18:24 AM, you wrote:

> I believe I have it working. Anyone see any blatant problems with this?
> Also, would it make sense to have this functionality in the engine itself?
> Seeing as that internal classes have inheritance capabilities, it seems
> logical to have the functionality to call a parent constructor within the
> engine itself. I could definitely see this being useful within the SPL as
> its class library continues to grow as well.

> void pow_call_parent_constructor(zval *object TSRMLS_DC)
> {
> zend_class_entry *ce = EG(function_state_ptr)->function->common.scope;

> if (ce->parent && ce->parent->constructor) {
> zval *retval_ptr;
> zend_fcall_info fci;
> zend_fcall_info_cache fcc;

> if (!(ce->parent->constructor->common.fn_flags & ZEND_ACC_PUBLIC)) {
> return;
> }

> fci.size = sizeof(fci);
> fci.function_table = NULL;
> fci.function_name = NULL;
> fci.symbol_table = NULL;
> fci.object_pp = &object;
> fci.retval_ptr_ptr = &retval_ptr;
> fci.param_count = 0;
> fci.params = NULL;
> fci.no_separation = 1;

> fcc.initialized = 1;
> fcc.function_handler = ce->parent->constructor;
> fcc.calling_scope = ce;
> fcc.object_pp = &object;

> if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
> return;
> }
> if (retval_ptr) {
> zval_ptr_dtor(&retval_ptr);
> }
> }
> }

> Forgive me if its crude, but it appears to work.

> Bob


> -----Original Message-----
> From: Bob Silva
> Sent: Friday, October 28, 2005 7:35 PM
> To: intern***@*ists.php.net
> Subject: Follow the object constructor chain ... options

> What would be the suggested method of simulating parent::__construct in
> C-level objects?

> I looked through the source and could find no examples to draw from.

>

> Since my base object is the foundation of my extension, its constructor has
> to be called on each derived object creation.

>

> I have a few ideas that have to do with looping through ce->parent and
> zend_call_function.

> Was wondering if you guys can offer some of your experience on how you would
> approach this problem.

>

> Bob Silva




Best regards,
Marcus

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#4 Oct. 29, 2005 18:58:37

Bob S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Follow the object constructor chain ... options


Why so complex? Because I have no clue what I am doing, but why let a little
thing like that stop me.

Thanks for the suggestion, I had to modify it a bit to properly climb the
constructor chain though. You cant grab the ce of the active object since it
may have multiple parents up the chain. The ce has to be the class_entry of
the executing function.

void pow_call_parent_constructor(zval *object TSRMLS_DC)
{
zend_class_entry *ce = get_active_class_entry(TSRMLS_C);
zend_function *ctor = ce->parent ? ce->parent->constructor : NULL;

if (ctor) {
zend_call_method_with_0_params(&object, ce, &ctor,
ZEND_CONSTRUCTOR_FUNC_NAME, NULL);
}
}

My C-level class structure is something like:

ZObject
ZValueType
ZString
ZInt
...

With this function, when you do $str = new ZString(); it will make sure that
the constructor chain (ZString::__construct -> ZValueType::__construct ->
ZObject::__construct) is followed, hence having to be the executing class
scope rather then the object class scope.



Many thanks for your help Marcus.

Bob


> -----Original Message-----
> From: Marcus Boerger
> Sent: Saturday, October 29, 2005 3:58 AM
> To: Bob Silva
> Cc: intern***@*ists.php.net
> Subject: Re: Follow the object constructor chain ... options
>
> Hello Bob,
>
> why so complex and potential wrong?
>
> zend_class_entry *ce = Z_OBJ_CE(object);
> zend_function *ctor = ce->parent ? ce->parent->constructor : NULL;
>
> if (ctor && ctor->common.fn_flags & ZEND_ACC_PUBLIC) {
> zend_call_method_with_0_params(object, ce, ctor, "__construct", NULL);
> }
>
> btw, why public? the parent ctor must only be called from a derived
> class instance, thus you want to check against !private. But in that
> case either the class or the ctor should be final and thus you shouldn't
> have this issue.
>
> marcus
>
> Saturday, October 29, 2005, 9:18:24 AM, you wrote:
>
> > I believe I have it working. Anyone see any blatant problems with this?
> > Also, would it make sense to have this functionality in the engine
> itself?
> > Seeing as that internal classes have inheritance capabilities, it seems
> > logical to have the functionality to call a parent constructor within
> the
> > engine itself. I could definitely see this being useful within the SPL
> as
> > its class library continues to grow as well.
>
> > void pow_call_parent_constructor(zval *object TSRMLS_DC)
> > {
> > zend_class_entry *ce = EG(function_state_ptr)->function-
> >common.scope;
>
> > if (ce->parent && ce->parent->constructor) {
> > zval *retval_ptr;
> > zend_fcall_info fci;
> > zend_fcall_info_cache fcc;
>
> > if (!(ce->parent->constructor->common.fn_flags &
> ZEND_ACC_PUBLIC)) {
> > return;
> > }
>
> > fci.size = sizeof(fci);
> > fci.function_table = NULL;
> > fci.function_name = NULL;
> > fci.symbol_table = NULL;
> > fci.object_pp = &object;
> > fci.retval_ptr_ptr = &retval_ptr;
> > fci.param_count = 0;
> > fci.params = NULL;
> > fci.no_separation = 1;
>
> > fcc.initialized = 1;
> > fcc.function_handler = ce->parent->constructor;
> > fcc.calling_scope = ce;
> > fcc.object_pp = &object;
>
> > if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
> > return;
> > }
> > if (retval_ptr) {
> > zval_ptr_dtor(&retval_ptr);
> > }
> > }
> > }
>
> > Forgive me if its crude, but it appears to work.
>
> > Bob
>
>
> > -----Original Message-----
> > From: Bob Silva
> > Sent: Friday, October 28, 2005 7:35 PM
> > To: intern***@*ists.php.net
> > Subject: Follow the object constructor chain ... options
>
> > What would be the suggested method of simulating parent::__construct in
> > C-level objects?
>
> > I looked through the source and could find no examples to draw from.
>
> >
>
> > Since my base object is the foundation of my extension, its constructor
> has
> > to be called on each derived object creation.
>
> >
>
> > I have a few ideas that have to do with looping through ce->parent and
> > zend_call_function.
>
> > Was wondering if you guys can offer some of your experience on how you
> would
> > approach this problem.
>
> >
>
> > Bob Silva
>
>
>
>
> Best regards,
> Marcus
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit:http://www.php.net/unsub.php--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#5 Oct. 29, 2005 19:08:25

Marcus B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Follow the object constructor chain ... options


Hello Bob,

we are btw thinking of having a way to enfore calling parent ctor.

anyway that looks like you're heading towards some autoboxing feature.
Even though it is not necessary aas in java it may gives you the ability
to type check with base types as well and given the assumption you implement
get/set handlers it should even interoperatoe with php's autoconversion.
If you have more to show i am curious :-)

marcus

Saturday, October 29, 2005, 7:57:45 PM, you wrote:

> Why so complex? Because I have no clue what I am doing, but why let a little
> thing like that stop me.

> Thanks for the suggestion, I had to modify it a bit to properly climb the
> constructor chain though. You cant grab the ce of the active object since it
> may have multiple parents up the chain. The ce has to be the class_entry of
> the executing function.

> void pow_call_parent_constructor(zval *object TSRMLS_DC)
> {
> zend_class_entry *ce = get_active_class_entry(TSRMLS_C);
> zend_function *ctor = ce->parent ? ce->parent->constructor : NULL;

> if (ctor) {
> zend_call_method_with_0_params(&object, ce, &ctor,
> ZEND_CONSTRUCTOR_FUNC_NAME, NULL);
> }
> }

> My C-level class structure is something like:

> ZObject
> ZValueType
> ZString
> ZInt
> ...

> With this function, when you do $str = new ZString(); it will make sure that
> the constructor chain (ZString::__construct -> ZValueType::__construct ->
> ZObject::__construct) is followed, hence having to be the executing class
> scope rather then the object class scope.



> Many thanks for your help Marcus.

> Bob


>> -----Original Message-----
>> From: Marcus Boerger
>> Sent: Saturday, October 29, 2005 3:58 AM
>> To: Bob Silva
>> Cc: intern***@*ists.php.net
>> Subject: Re: Follow the object constructor chain ... options
>>
>> Hello Bob,
>>
>> why so complex and potential wrong?
>>
>> zend_class_entry *ce = Z_OBJ_CE(object);
>> zend_function *ctor = ce->parent ? ce->parent->constructor : NULL;
>>
>> if (ctor && ctor->common.fn_flags & ZEND_ACC_PUBLIC) {
>> zend_call_method_with_0_params(object, ce, ctor, "__construct", NULL);
>> }
>>
>> btw, why public? the parent ctor must only be called from a derived
>> class instance, thus you want to check against !private. But in that
>> case either the class or the ctor should be final and thus you shouldn't
>> have this issue.
>>
>> marcus
>>
>> Saturday, October 29, 2005, 9:18:24 AM, you wrote:
>>
>> > I believe I have it working. Anyone see any blatant problems with this?
>> > Also, would it make sense to have this functionality in the engine
>> itself?
>> > Seeing as that internal classes have inheritance capabilities, it seems
>> > logical to have the functionality to call a parent constructor within
>> the
>> > engine itself. I could definitely see this being useful within the SPL
>> as
>> > its class library continues to grow as well.
>>
>> > void pow_call_parent_constructor(zval *object TSRMLS_DC)
>> > {
>> > zend_class_entry *ce = EG(function_state_ptr)->function-
>> >common.scope;
>>
>> > if (ce->parent && ce->parent->constructor) {
>> > zval *retval_ptr;
>> > zend_fcall_info fci;
>> > zend_fcall_info_cache fcc;
>>
>> > if (!(ce->parent->constructor->common.fn_flags &
>> ZEND_ACC_PUBLIC)) {
>> > return;
>> > }
>>
>> > fci.size = sizeof(fci);
>> > fci.function_table = NULL;
>> > fci.function_name = NULL;
>> > fci.symbol_table = NULL;
>> > fci.object_pp = &object;
>> > fci.retval_ptr_ptr = &retval_ptr;
>> > fci.param_count = 0;
>> > fci.params = NULL;
>> > fci.no_separation = 1;
>>
>> > fcc.initialized = 1;
>> > fcc.function_handler = ce->parent->constructor;
>> > fcc.calling_scope = ce;
>> > fcc.object_pp = &object;
>>
>> > if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
>> > return;
>> > }
>> > if (retval_ptr) {
>> > zval_ptr_dtor(&retval_ptr);
>> > }
>> > }
>> > }
>>
>> > Forgive me if its crude, but it appears to work.
>>
>> > Bob
>>
>>
>> > -----Original Message-----
>> > From: Bob Silva
>> > Sent: Friday, October 28, 2005 7:35 PM
>> > To: intern***@*ists.php.net
>> > Subject: Follow the object constructor chain ... options
>>
>> > What would be the suggested method of simulating parent::__construct in
>> > C-level objects?
>>
>> > I looked through the source and could find no examples to draw from.
>>
>> >
>>
>> > Since my base object is the foundation of my extension, its constructor
>> has
>> > to be called on each derived object creation.
>>
>> >
>>
>> > I have a few ideas that have to do with looping through ce->parent and
>> > zend_call_function.
>>
>> > Was wondering if you guys can offer some of your experience on how you
>> would
>> > approach this problem.
>>
>> >
>>
>> > Bob Silva
>>
>>
>>
>>
>> Best regards,
>> Marcus
>>
>> --
>> PHP Internals - PHP Runtime Development Mailing List
>> To unsubscribe, visit:http://www.php.net/unsub.phpBest regards,
Marcus

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

  • Root
  • » PHP
  • » [PHP-DEV] Follow the object constructor chain ... options [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support