Classes & Passing an array as an argument

From: Date: Mon, 03 Jul 2000 02:42:19 +0000
Subject: Classes & Passing an array as an argument
Groups: php.general 
Request: Send a blank email to php-general+get-4428@lists.php.net to get a copy of this message
Hello, I'm not the best programmer in the world, but I'm writing a class that lets me reuse a lot of code that I find myself dragging out over and over again. What I'd like to do is be able to set values for properties when I create a new object, but I only know how to do it the clumsy way. I.e. This is a simplified example: <? class Foo { var $font_face; var $font_size; var $font_color; function Set($name, $value) {
     $this->$name = $value;
} function Text($text) {
     $junk  = "<font face=\"$this->font_face\">";
     $junk .= "$text</font>\n";
     return $junk;
} } ?> So, to set the font face, I'd have to: $foo = new Foo; $foo->Set('font_face','Helvetica'); $foo->Text('Bar'); But, using that, I'd have to call $foo->Set() to set each of my properties. I'd like to be able to something like: $foo->Set(
    'font_face'  => 'Helvetica',
    'font_size'  => '3',
    'font_color' => '#FFFFFF'
); But I can't even begin to think up a way to accomplish that, but I need to learn how. How would I pass an array to the method Set() ( without saying something like $foo->Set(array()) )? Having to give values to public (?) properties in a class would kind of defeat the purpose of using a class wouldn't it? In other words, defining $font_face as "Verdana" within the class itself would mean that I would have to change that within the class itself if I wanted to change the font face. Thanks for a how-to, Brian

« previous php.general (#4428) next »