calling a constructor of an extended class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

calling a constructor of an extended class

Post by cybaf »

Maybe i'm just going at this problem in the wrong way (or thinking too much java) but how would I proceed with the following?

Code: Select all

<?php
class Foo extends Bar {
    function Foo($args='some argument') {
        // do something
    }

    //some other functions
}

class Bar {
    function Bar($arg1=null,$arg2=null) {
        //do some vital stuff
    }
}
?>
How is the Bar constructor called? or do I just to Bar::Bar("arg1","arg2); ??
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

If class Foo has no constructor then class Bar's constructor would be called automatically.
To call class Bar's constructor from within class Foo's constructor do
parent::Bar();

See http://php.net/manual/en/keyword.parent.php for more info ;)
Last edited by markl999 on Sat Oct 18, 2003 9:57 am, edited 1 time in total.
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

ahh sweet...:)

tried searching php.net for parent before posting, but it didn't lead me there... well well thanks!
Jay
Forum Newbie
Posts: 22
Joined: Fri Jul 12, 2002 8:36 am
Location: KUL, Malaysia

Post by Jay »

or even simply: $this->Bar();
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Jay wrote:or even simply: $this->Bar();
A small point: this will fall down if you have a method Bar() in Foo().

Cybaf: Bar::Bar() works OK but, as mentioned above, parent::Bar() is better in case you later change the name of the Bar class.

This is probably the link you were looking for:

http://uk.php.net/oop
Jay
Forum Newbie
Posts: 22
Joined: Fri Jul 12, 2002 8:36 am
Location: KUL, Malaysia

Post by Jay »

Here's an even smaller point: If you change the name of the Bar class, how does parent::Bar() still work?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Jay wrote:Here's an even smaller point: If you change the name of the Bar class, how does parent::Bar() still work?
:oops: I should think before I post.

If parent::method() was aimed at a method and not the constructor it would survive a name change. If pointed at the constructor, and you changed the name of the class (but not the constructor) it would still work - but of course the constructor name is likely to be changed also as you rightly point out.
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

hehe good points... and some less thought through...:) McGruff: I found that link you gave me before I posted, but I guess I skimmed through it a bit too fast.

A big problem when searching for solutions is that no matter how much I look, I will allways get a better solution here at this awesome forum! :)

the above point about calling parent::method() when method() is the constructor will all be solved with php5... with the nice __construct() default method... then we can change the names as much as we want.
Post Reply