Jump to content

Overloading Constructors


KittyKate

Recommended Posts

I have a class that may or may not be passed a parameter. How do I deal with this?

My first inclination was to declare two constructors. However, this gives me the error: Fatal error: Cannot redeclare supportclass() in "..." on line ...
[code]function supportClass() {
$htmlGenSupport = new htmlGen();
}

function supportClass($html) {
$this->htmlGenSupport = $html;
}[/code]

Knowing PHP lets you overload functions, I tried this, which gives me the error: Parse error: parse error in "..." on line ...
[code]function supportClass($html = new htmlGen()) {
$this->htmlGenSupport = $html;
}[/code]

Any suggestions?
Link to comment
Share on other sites

[quote author=KittyKate link=topic=101407.msg401308#msg401308 date=1153503257]
Knowing PHP lets you overload functions, I tried this, which gives me the error: Parse error: parse error in "..." on line ...
[/quote]

[quote author=PhpManual]
PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.
http://www.php.net/manual/en/language.functions.php
[/quote]

The exception to this being overloading methods in children. eg:

[code]<?php
class myclass
{
    function method()
    {
        //dostuff
    }
}

class myotherclass extends myclass
{
    function method()
    {
        //dostuff
        parent::method();
        // Call the parents method named method()
    }
}
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.