Hey..just wondering if i can have 2 constructors? if so, would I just have:


class TreeViewNode{

public $child;
public $parent = ""; 
public $key = "";
public $data = "";
public $next;
public $section;

function __construct($k, $p, $d)
    {

 $this->parent = $p;
     $this->key = $k;
     $this->data = $d;

}

function __construct($k, $p, $d,$s)
    {

     $this->parent = $p;
     $this->key = $k;
     $this->data = $d;
     $this->section = $s;

}

//all other class functionos

}


would I just do it just like that?

edit** I read something....is this the right way to do it, doesnt seem that correct 😛

function __construct($k, $p, $d, $s='')
    {

	 $this->parent = $p;
     $this->key = $k;
     $this->data = $d;
     $this->section = $s;

}

thanks!

    PHP does not support function overloading.

      The second piece of code makes use of default values . If you create a TreeViewNode with 3 parameters it will use an empty string or whatever you set $s equal to. If you call it with 4 parameters, then it will use the fourth parameter and pass it to $s.

      The best advice i can give you is "try it and see".

      I hope that helps.

        No point in trying, PHP does not support this.

        Look into creation patterns, like the Factory pattern, for simulated function overloading.

          so , the second piece of code will not work?

            The code after the OP's edit will run. It uses default parameter values, which PHP does support. Function overloading, the original question, is not supported.

              Kudose;10926402 wrote:

              The code after the OP's edit will run. It uses default parameter values, which PHP does support. Function overloading, the original question, is not supported.

              great, thanks! tried it and it works...i just wanted to make sure i wasnt doing anything accidentally wrong 🙂

              Paul

                mpb001;10926420 wrote:

                His method is sometimes more elegant even in languages that support overloading as it is a valid refactor for the sake of improved readability.

                "Replace Constructors with Creation Methods"
                http://www.industriallogic.com/xp/refactoring/constructorCreation.html

                Hmm...I dont really get this way 😛 I will just stick with the other way 🙂 it works anyways, which is the important part.

                thanks everyone!

                  Toadums;10926450 wrote:

                  Hmm...I dont really get this way 😛 I will just stick with the other way 🙂 it works anyways, which is the important part.

                  thanks everyone!

                  I am kicking myself pretty hard currently over this mentality.

                    Kudose;10926470 wrote:

                    I am kicking myself pretty hard currently over this mentality.

                    what..? it works, and it works well. I dont really care about elegance, I am no pro php coder.

                      Toadums wrote:

                      I dont really care about elegance,

                      Elegance is important. Without it you end up with crap. Even if it's crap that "works" (for now).

                        ok, ok. perhaps I will google around the topic suggested, try to figure it out hehe

                        can you tell me how this code, however, is not elegant? perhaps I am not fully understanding the word in this context

                        function __construct($k, $p, $d, $s='')
                            {
                        
                             $this->parent = $p;
                             $this->key = $k;
                             $this->data = $d;
                             $this->section = $s;
                        
                        } 

                        thanks 🙂

                          I'm just picturing what would happen as soon as you need another optional argument.

                            Weedpacket;10926562 wrote:

                            I'm just picturing what would happen as soon as you need another optional argument.

                            it wont work if I need 2?

                              And I want to specify $c but not $s (whatever $c and $s are - $s is short for "section" because $section is too much trouble to type, so $c must be....)?

                                Write a Reply...