Re: Object Constructors -- Part II

From: Date: Fri, 15 Sep 2000 19:56:57 +0000
Subject: Re: Object Constructors -- Part II
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-16984@lists.php.net to get a copy of this message
The Captain writes: > Well, it turns out that it was my own terrible typo that was causing the > error on a constructor with passed variables. > > However... I am getting another error with constructor overloading. > > Here's a code sample: > > class openFile { > var $myfile; > > function openFile() { > $this->myFile = "/var/logs/somefile"; > } > > function openFile($f) { > $this->myFile = $f; > } > > } > > > this is how i'm newing the object: > > $of = new of; > > I am getting this error from PHP though: > > Warning: Missing argument 1 for log() in log.inc on line 8 > > I wanted to have both constructors in there so I could have a "default" > filename as well as one i could specify when the object gets created. > > Any ideas on what's happening here? > > Thanks PHP doesn't support overloading that way--that looks a little C++ey to me. :) PHP does, however, support default arguments, so you could do something like this: <?php class openFile { var $filename; var $fp; function openFile($f = "/var/logs/log_file") { $this->filename = $f; } } $of = new openFile(); echo $of->filename; ?> You could also use func_get_args() and friends to allow the function to accept an arbitrary number of arguments. Note that PHP's classes are not true OOP classes; they are more or less a different syntax for arrays which can contain functions as well as data. See the following pages for more information: http://www.php.net/manual/functions.arguments.php http://www.php.net/manual/function.func-get-args.php http://www.php.net/manual/function.func-num-args.php http://www.php.net/manual/function.func-get-arg.php Hope this helps, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#16984) next »