Reputation: 643
I very rarely use classes in PHP so forgive my ignorance.
I've got a class with various functions in it with return values. At the beginning of the class, I've got a constructer for creating variables used in the class functions. Like so:
function __construct($firstVariable,$secondVariable,$thirdVariable) {
if(isset($firstVariable)) { $this->firstname = $first; };
if(isset($secondVariable)) { $this->secondname = $second; };
if(isset($thirdVariable)) { $this->thirdname = $third; };
}
My question is thus: what if I only intend to use $secondVariable
? I understand that I can do the following upon class instantiation:
$Class = new classname(NULL,$secondVariable,NULL);
But I feel like this is improper or inefficient. With this method, I literally need to pass NULL
every time I don't want to use a variable... this would be happening a lot as I use variations of the class from page to page. For example, page #1 uses the second parameter but page #2 uses the third. #3 uses all three.
So...
#1: $Class = new classname(NULL,$secondVariable,NULL);
#2: $Class = new classname(NULL,NULL,$thirdVariable);
#3: $Class = new classname(#firstVariable,$secondVariable,$thirdVariable);
Well, that's great and all but what if I add a new function in the class which requires its own variable and therefore a fourth parameter. I would need to go back and add 'NULL' as the fourth parameter on all of the class instantiations where this new function wasn't being used (and to precent php from throwing an error since the class expects a fourth parameter). Surely that can't be PHP best practices!
Upvotes: 1
Views: 110
Reputation: 1370
You can use default argument to fulfil your needs.
see LIVE demo
function __construct($firstVariable=NULL,$secondVariable=NULL,$thirdVariable=NULL) {
if(isset($firstVariable)) { $this->firstname = $first; };
if(isset($secondVariable)) { $this->secondname = $second; };
if(isset($thirdVariable)) { $this->thirdname = $third; };
}
Upvotes: 1
Reputation: 32912
If you want to skip the arguments from the last to the first, use BT643 answer. But if you want to use only the second and skip the previous, you should use factory method pattern:
class YourClass {
function __construct($firstVariable,$secondVariable,$thirdVariable) {
// define the object here
}
static function createWithSecond($secondVariable) {
return new YourClass(NULL,$secondVariable,NULL);
}
}
// the client code
$obj1 = new YourClass(1,2,3); // use constructor
$obj2 = YourClass::createWithSecond(2); // use factory method
Upvotes: 1
Reputation: 3845
This should work, I think?
function __construct($firstVariable=NULL,$secondVariable=NULL,$thirdVariable=NULL) {
if(isset($firstVariable)) { $this->firstname = $first; };
if(isset($secondVariable)) { $this->secondname = $second; };
if(isset($thirdVariable)) { $this->thirdname = $third; };
}
Then if you add more parameters, they'll default to NULL unless otherwise specified. Do note though, even an empty string will overwrite the default NULL.
So for your example of only using the $secondVariable
, you could just do: $Class = new classname(NULL,$secondVariable);
. The rest would be defaulted to NULL automatically.
If you then changed your function to include more variables:
function _construct($firstVariable=NULL,$secondVariable=NULL,$thirdVariable=NULL,$fourthVariable=NULL) {
It wouldn't cause any issues.
Upvotes: 3