Is there any way to implement isset() in a setter method? That is, have the setter method check if the variable exists? From what I can tell no, but hopefully someone can confirm that for me. In a nutshell, I’m looking to avoid having to do
if (isset($arr[0])) $foo->setId($arr[0])
and simply just do
$foo->setId($arr[0])
and somehow implement the isset() logic in the setter method. Thanks!
No, you not can subscript a missing array element and expect it to work. The subscript will be evaluated before being sent to the method. This means the subscript will fail before the method even receives the argument.
Example
If I do
$foo->setId($arr[0])then$foo->setId()will receiveaas a string, and it will never know it was subscripted from an array (not does it want to know).So a
$foo->setId($arr[3])would give an error…