Women in Technology

Hear us Roar



Article:
  Object Overloading in PHP 5
Subject:   __construct overloading
Date:   2006-07-24 02:07:36
From:   abc@yahoo.com
why the following code gives error!
<?
class A
{
public function __construct()
{
echo "hye";
}
public function __construct($p)
{
echo $p,"<br />";
}
}
$ob1 = new A(5);
$ob2 = new A();
?>


Full Threads Newest First

Showing messages 1 through 3 of 3.

  • __construct overloading
    2006-07-24 08:25:38  MartinJansen [View]

    Multiple constructors per class are not supported. You'll have to use something like


    <?php
    ...

    public function __construct($p = null) {
    if ($p === null) {
    echo "hye";
    } else {
    echo $p . "<br />";
    }
    }

    ...
    ?>
    • __construct overloading
      2007-02-19 05:15:53  ambikadevi [View]

      The great drawback in PHP is there is no constructor/function overloading.
      • __construct overloading
        2007-03-13 04:56:42  Martynas [View]

        are you sure? there seem to be work-arounds, at least: http://snippets.dzone.com/posts/show/3237