php class and constructor

druagord's picture

He has: 335 posts

Joined: May 2003

Ok I just reread the constructor section of php.net and there's one thing i want to be sure i understood well. When creating a class that extends another one in php4 is the constructor of the extended class called only when there is no constructor in the extend class or is called anyway.

what about the register_shutdown_function if it is defined in the extended class will it be called when the extend class will be terminated.

Thanks for any help.

IF , ELSE , WHILE isn't that what life is all about

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I got confused there for a bit, so I'm gonna say "Class 'B' Extends Class 'A'".

When creating an instance of class 'B', the constructor for class 'B' will be called. The constructor for 'A' will not be called. This is simply because the constructor for a class is always a function of the same name.

I haven't anything linking register_shutdown_function() to classes. It's a global thing.

I'm guessing you have a function (destructor?) defined in class 'A', and want to know if register_shutdown_function() will call it if you have an instance of class 'B'. I believe it will.

Mark Hensler
If there is no answer on Google, then there is no question.

druagord's picture

He has: 335 posts

Joined: May 2003

ok understood for the destructor but for the constructor i know that if class B doesn't have one the one from class A will be called since i already done something like that now i want to know what will happen if class B also as a constructor should i call the one from class A in the one from class B or not (i don't want it to be called twice). I guess i'll just build some dummy class to experiment.

Thank's for the answer.

IF , ELSE , WHILE isn't that what life is all about

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

So you have a class 'B' with no constructor and when creating an instance of class 'B', the constructor for class 'A' was called?

I guess that does make sense. Because class 'A' may require data to instanciate (is that a word?) itself.

Mark Hensler
If there is no answer on Google, then there is no question.

druagord's picture

He has: 335 posts

Joined: May 2003

Mark Hensler wrote: So you have a class 'B' with no constructor and when creating an instance of class 'B', the constructor for class 'A' was called?

Yes actualy i'm improving on this code and now need to set some other variables in class B And in class C wich also extends class A

Mark Hensler wrote:
I guess that does make sense. Because class 'A' may require data to instanciate (is that a word?) itself.

yes i think it's the word. From the php.net docs i know this works only in php4 and not in php3. Since i'm only designing my new class structures right now i didn't experiment yet but i'll keep you posted on my findings. Thanks again for your help.

IF , ELSE , WHILE isn't that what life is all about

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

You might want to consider php5. I just upgraded my server, and I'm looking forward to developing with the new object stuff.

http://www.php.net/zend-engine-2.php

Mark Hensler
If there is no answer on Google, then there is no question.

druagord's picture

He has: 335 posts

Joined: May 2003

yes i took a look at it. and it seems very nice i just want it to get a little more mature before i upgrade my production servers. I will install it on my workstation as soon as i find time for that Smiling

IF , ELSE , WHILE isn't that what life is all about

druagord's picture

He has: 335 posts

Joined: May 2003

ok finally i don't know why i made such a big fuzz out of this. When there is a constructor for class B the constructor for class A is not called. I simply added a line to class B contructor wich call Class A constructor

IF , ELSE , WHILE isn't that what life is all about

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.