Reputation: 14260
I am trying to create properties based on the method arguments. For example:
class Test{
public function newProperty($prop1,$prop2){
//I want to create $this->argu1 and $this->argu2 after calling newProperty method.
}
}
$test=new Test();
$test->newProperty('argu1','argu2')
Is this possible? Thanks for any helps.
Upvotes: 0
Views: 89
Reputation: 54649
as simple as:
$this->$prop1 = 'whatever';
suppose you wanted to handle an undefined number of arguments, you could use:
foreach(func_get_args() as $arg) {
$this->$arg = 'some init value';
}
On the other hand, all this is quite unnecessary as all these properties will be public and thus:
$test->argu1 = 'whatever';
would do exactly the same.
Upvotes: 3
Reputation: 26380
Try this:
class Test{
private argu1 = '';
private argu2 = '';
public function newProperty($argu1,$argu2){
//This is a great place to check if the values supplied fit any rules.
//If they are out of bounds, set a more appropriate value.
$this->prop1 = $argu1;
$this->prop2 = $argu2;
}
}
I'm a little unclear from the question if the class properties should be named $prop or $argu. Please let me know if I have them backwards.
Upvotes: 1