Good.
I want to assign attributes to a class from an array that may or may not have the keys, like in this example:
class ClasePrueba extends Model {
public function fillWithData(array $data){
$this->ATRIBUTE1 = $data['data1'];
$this->ATRIBUTE2 = $data['data2'];
$this->ATRIBUTE3 = $data['data3'];
$this->ATRIBUTE4 = $data['data4'];
}
}
but if the key data1
or any of the others don't exist, then I get an error of access to index not defined.
Notice: Undefined offset: data1
You could try a code like the following
$this->ATRIBUTE1 = (isset($data['data1'])? $data['data1']: NULL;
But I have noticed that the attribute is created and left as NULL. So I try an "assign" method that passes by reference the model attribute and the reference to the array element that may or may not be there, this is an example:
<?php
$array = array("1" => "PHP");
function assign(&$to, &$data){
if(isset($data)){
$to = $data;
}
}
class a {
}
$testClass = new a;
var_dump(isset($testClass->attribute )); // bool(false)
var_dump($testClass->attribute); // Notice: Undefined property: a::attribute
assign($testClass->attribute, $array["5"]);
var_dump($testClass->attribute); // NULL
var_dump(is_null($testClass->attribute )); // bool(true)
var_dump(isset($testClass->attribute )); // bool(false)
var_dump($testClass);
So when I call the first one var_dump
the result is expected, the variable $testClass->attribute
is not defined.
Then on the second I try to access, but the PHP interpreter notifies me that the variable $testClass->attribute
is not defined, also as expected.
I call the function assing
with the class attribute as a parameter and with $array["5"]
it being an undefined key, therefore the assignment inside the function is never executed assign
, and therefore the object property is not created (in theory)
Then everything explodes for me when I call the third time var_dump
and it returns NULL
. I don't understand why it returns NULL in the following calls instead of notifying that the attribute in the instance $testClass
is not defined. It is as if the attribute were assigned NULL in the following way:
$testClass = new a;
var_dump(isset($testClass->attribute )); // bool(false)
$testClass->attribute = NULL;
var_dump($testClass->attribute); // NULL
and the last var_dump() returns:
object(a)#1 (1) {
["attribute"]=>
NULL<br>
}
So it is as if my variables are set to NULL when using them as parameters in a function by reference, instead of reporting that they are not defined as if I were using unset()
, since I 'technically' never set them to NULL.
This is my question: why is this happening?
"If a variable not defined by reference is assigned, passed, or returned, the variable will be created." ... Source: http://php.net/manual/en/language.references.whatdo.php
Cheers!
About your mind blast:
$array["5"]
it's not defined, when you try to access an undefined variable, instead of getting its content or reference, PHP gives younull
.Something you should consider, it seems that you expect the expression
$array["5"]
to be given to the function and to be evaluated within it, but the expressions that are passed as arguments must be evaluated before being given to the functions, for the same reason, what PHP sees, it is:If the assignment is executed, inside
assign
, the variable$data
is set to null.About the following
var_dump
:From the documentation of
isset
It is correct that it
isset($testClass->attribute)
returns false.