constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alain dhaene

    constructors

    Hi,

    Is it possible to have more constructors in the same class?

    e.g

    class person
    {
    function person($name)
    {
    }

    function search($name,$a ge)
    {
    }

    }


    //make object

    $person1 = new person("genesis ");
    $person2 = new person("U2",40) ;

    thx.

    Alain


  • Alain Dhaene

    #2
    Re: constructors

    oeps,
    That's wrong it must be:

    class person[color=blue]
    > {
    > function person($name)
    > {
    > }
    >
    > function person($name,$a ge)
    > {
    > }
    >
    > }
    > //make object
    >
    > $person1 = new person("genesis ");
    > $person2 = new person("U2",40) ;
    >
    > thx.[/color]



    "alain dhaene" <a.dhaene@instr uct.be> schreef in bericht
    news:4014f103$0 $7036$ba620e4c@ news.skynet.be. ..[color=blue]
    > Hi,
    >
    > Is it possible to have more constructors in the same class?
    >
    > e.g
    >
    > class person
    > {
    > function person($name)
    > {
    > }
    >
    > function search($name,$a ge)
    > {
    > }
    >
    > }
    >
    >
    > //make object
    >
    > $person1 = new person("genesis ");
    > $person2 = new person("U2",40) ;
    >
    > thx.
    >
    > Alain
    >
    >[/color]


    Comment

    • Berislav Lopac

      #3
      Re: constructors

      alain dhaene wrote:[color=blue]
      > Is it possible to have more constructors in the same class?[/color]

      As I understand it, all methods in PHP follow the general functions rule,
      which is that you can't redeclare a function. Unlike Java, where a method's
      signature includes the arguments and the return type besides the name
      itself, in PHP the "signature" is only the method's name.

      However, you can "overload" a function by stating optional arguments, which
      are defined in fuction definition, but can be avoided when calling the
      function/method.

      In your example, the constructor could be:

      person($name, $age = NULL)

      and your two example calls would work.

      Berislav


      Comment

      • Jochen Buennagel

        #4
        Re: constructors

        Berislav Lopac wrote:
        [color=blue]
        > In your example, the constructor could be:
        >
        > person($name, $age = NULL)
        >
        > and your two example calls would work.[/color]

        If you need different kinds of arguments for different constructors
        (e.g. person($name) and person($sex, $age)), you can also do the following:

        class person {
        function person(){
        switch (func_num_args( )) {
        case 1:
        $this->name = func_get_arg(0) ;
        break;
        case 2:
        $this->sex = func_get_arg(0) ;
        $this->age = func_get_arg(1) ;
        break;
        default:
        die ('wrong number of arguments for constructor');
        }
        }
        }

        You could further check the types of the arguments in different places,
        e.g. if func_get_arg(0) is a string of "m" or "f", treat the call as
        person($sex, $age), otherwise treat it as person($name, $age)

        Jochen

        Comment

        Working...