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
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
   }
}

Link to comment
Share on other sites

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
));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.