dmikester1 Posted March 15, 2012 Share Posted March 15, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259004-calling-a-class-with-several-variables/ Share on other sites More sharing options...
KevinM1 Posted March 15, 2012 Share Posted March 15, 2012 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/259004-calling-a-class-with-several-variables/#findComment-1327785 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 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 )); Quote Link to comment https://forums.phpfreaks.com/topic/259004-calling-a-class-with-several-variables/#findComment-1327787 Share on other sites More sharing options...
dmikester1 Posted March 15, 2012 Author Share Posted March 15, 2012 Makes sense, I might got the array route. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/259004-calling-a-class-with-several-variables/#findComment-1327808 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.