Re: Overloaded constructors in Classes???
| From: | Max Derkachev | Date: | Fri, 27 Oct 2000 08:10:10 +0000 |
| Subject: | Re: Overloaded constructors in Classes??? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-22293@lists.php.net to get a copy of this message | ||
1. No, you can't do i t like this. There's no ability to repeat function
names in one namespace in PHP.
2. How could You know, that the arguments passed to foo::foo are $a and
$b, not $c and $d ? You should pass all 4 args to determine what
variables are actually passed.
You can set the default argument values, and then test them. In this
case the functionality You need could be achieved this way:
class foo
{
var $a;
var $b;
var $c,
var $d;
function foo ($a=0, $b=0, $c=0, $d=0) {
if ($a && $b) $this->ab($a, $b);
if ($c && $d) $this->cd($c, $d);
...... //other stuff
}
function ab ($a, $b) {
//set $a and $b
}
function cd ($c, $d) {
//set $c and $d
}
}
You can also play with different argument count passed to the
constructor. Argument count could be get using func_num_args() within
the finction, and arguments could be accessed by func_get_arg
($argument_number).
Dallas Kropka wrote:
> Actually I was thinking of this,....
>
> class foo
> {
> var $a;
> var $b;
> var $c;
> var $d;
>
> function foo($a, $b)
> {
> assign $a and $b;
> }
> function foo($c, $d)
> {
> assign $c and $d
> }
> }
>
> Can you do this in the same class?
--
Best regards,
Max A. Derkachev mailto:kot@books.ru
Symbol-Plus Publishing Ltd.
phone: +7 (812) 265-0054, 265-1228, phone/fax: 567-8775
http://www.Books.Ru -- All Books of Russia