constructor overloading in php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • emrahayanoglu@gmail.com

    constructor overloading in php

    Hello Everyone,

    I looked everywhere for that problem. Does anyone know that how can i
    overload constructor function in php 5?

    Best Regards,

    Emrah Ayanoglu

  • Sjoerd

    #2
    Re: constructor overloading in php


    emrahayanoglu@g mail.com wrote:[color=blue]
    > Does anyone know that how can i
    > overload constructor function in php 5?[/color]

    Yes. PHP4 or PHP5? What have you tried already?

    Comment

    • emrahayanoglu@gmail.com

      #3
      Re: constructor overloading in php

      i tried like other languages. This is an example.
      PHP5

      class emr
      {
      function __construct($us ername)
      {
      }
      function __construct($us ername, $password)
      {
      }
      function __construct($us ername, $password, $name)
      {
      }
      }
      Then i have that error:Fatal error: Cannot redeclare emr::__construc t()

      Comment

      • Janwillem Borleffs

        #4
        Re: constructor overloading in php

        emrahayanoglu@g mail.com wrote:

        Don't multipost
        [color=blue]
        > i tried like other languages. This is an example.
        > PHP5
        >[/color]

        See my reply in alt.comp.lang.p hp; in short: Overloading like in Java is not
        supported in PHP.


        JW


        Comment

        • Rik

          #5
          Re: constructor overloading in php

          emrahayanoglu@g mail.com wrote:[color=blue]
          > i tried like other languages. This is an example.
          > PHP5
          >
          > class emr
          > {
          > function __construct($us ername)
          > {
          > }
          > function __construct($us ername, $password)
          > {
          > }
          > function __construct($us ername, $password, $name)
          > {
          > }
          > }[/color]

          If that's what you wnat, why not declare some defaults for the variables:
          function __construct($us ername, $password=false , $name=false)
          {
          if($name!==fals e)//execute some code
          if($password!== false)//domething else
          }
          Etc.
          Which makes you able to:
          $a = new emr('username') ;

          And all arguments left out will result in their default values.

          Else, it's up to the earlier mentioned func_get_args() ;

          Grtz,
          --
          Rik Wasmus


          Comment

          Working...