Jump to content

Overloading Constructor


Link

Recommended Posts

You can override a method of an extension of a class.  Example:

 

<?php
class Animal{
$this->name='Fred';
public function eat(){
	echo $this->name.' eats.';
}
}

class Dog extends Animal{
public function eat(){
	echo $this->name.' enjoys his Kibbles n Bits';
}
}
?>

Link to comment
Share on other sites

method overriding is not the same as method overloading.

 

Since you want to do this with a constructor it would look something like

 

<?php
class Foo
{
    public function __construct()
    {
        $all_args = func_get_args();
        
        if (count($all_args) == 1)  {
            $this->construct_one($all_args[0]);
        } else {
            $this->construct_two($all_args[0],$all_args[1]);
        }
    
    }
    
    protected function construct_one($a)
    {
        echo ($a);
    }
    
    protected function construct_two($a,$b)
    {
        echo ($a);
        echo ($b);
    }
}
<?

 

Obviously, that's not as cool.

Link to comment
Share on other sites

Actually have a look at the Reflection class that comes with PHP5.1 now

http://uk.php.net/oop5.reflection

I think there are several functions there you will be able to use to do method overloading (sort of).

 

Otherwise, just don't do it. Overloading is apparent in C++/C# and Java I know, but is it necessarily a good thing? Name your functions by what they're going to do and if you're expecting a variable number of parameters considering using a class helper object or an array (or worst of all default param values).

Link to comment
Share on other sites

It is a good think. It'll enable you to make polymorphic methods so you can create an interface which will act accordingly to the types given. Take for instance the + operator in a strictly typed language. If it wasn't overloaded then you'd need four operators instead of one (if we only take integer and float types into account): int + int, int + float, float + int and float + float. In some many languages it acts as a concatenation operator as well which will require additional overloading again. The reflection classes can however not add that type of overloading functionality. You'll have to use the magic __call() method for a kind of emulated overloading.

Link to comment
Share on other sites

To specify over what aschk and Daniel0, you can use the Reflection API to instantiate your class using a varying number of arguments as you would with call_user_func(). I'll leave it up to you to find which function that is in the manual.

 

And backing what Daniel0 said, overloading is convenient for a strictly-typed language. You should have some sort of an idea about which type your variable is that you're handling, therefore you should know what you're doing with your functions, be they operators or the general kind.

Link to comment
Share on other sites

  • 2 weeks later...

I understand the concept of polymorphism, I just think it goes against the idea of not re-typing code.

 

There are a very few circumstances I can see ever needing it, but how often do you need two functions with the same name? Two seperate functions could surely do in 99% of the cases?

 

Either with a third function of "shared code" (otherwise the functions are using the same code, twice the typing if you need to change anything) or they're so different they should be seperate.

Link to comment
Share on other sites

class B extends A {
  public function A () {
    // leave this blank to stop parent constructor executing if it's a php4 syntax class
  }
  public function __construct () {
    //this is your new constructor
  }
}

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.