Re: Object Constructors -- Part II
| From: | Rasmus Lerdorf | Date: | Fri, 15 Sep 2000 15:51:31 +0000 |
| Subject: | Re: Object Constructors -- Part II | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-16983@lists.php.net to get a copy of this message | ||
On Fri, 15 Sep 2000, The Captain wrote:
> 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.
This is a good example of a really silly thing people do with
overloading. The proper way to do this is:
class openFile {
var $myfile;
function openFile($f = "/var/logs/somefile") {
$this->myFile = $f;
}
}
Nice and clean.
-Rasmus