Jump to content

calling a class with several variables


dmikester1

Recommended Posts

Does anyone know if it's possible to call a class with an arbitrary number of variables?  I'll explain what I mean.

Here is my class construct definition:

function __construct($file, $outFile = 'newpdf.pdf', $fontSize = 70, $alpha=0.6, $overlayMsg = 'N O T  F O R  S H O P', $degrees = 45) {

 

I know that I have to pass over the $file variable for it to work now.  But can I create the class passing just the $file var and the $overlayMsg var?  Everything else I would want to leave as default.  How would I do that?

Thanks

Mike

Link to comment
https://forums.phpfreaks.com/topic/259004-calling-a-class-with-several-variables/
Share on other sites

Why not something like:

 

class Whatever {
   public $outFile = "newpdf.pdf";
   public $fontSize = 70;

   // etc. for the settings that will likely never change

   public function __construct($file, $overlayMsg = "N O T  F O R  S H O P", $options = NULL) {
      // $options is an optional ARRAY of, well, options that could override the existing defaults
   }
}

You would have to pass all of the variables up to $overlayMsg first.

 

If that's not what you want, then pass a single array or object instead.

function __construct($file, $data) {

 

$cls = new Cls('file.txt', array(
'outFile'    => 'newpdf.pdf',
'fontSize'   => 70,
'alpha'      => 0.6,
'overlayMsg' => 'N O T  F O R  S H O P',
'degrees'    => 45
));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.