I am working on fixing some bug in legacy php code. I am not a PHP person. so please bear with me if this is a stupid question.
I see this line in the code:
$credentials = array("username"=>$fields->username, "password"=>$fields->password);
$this->profile->create($fields);
I don't understand what this syntax for array means?
I know this is associate array:
array("username"=>"mike","password"=>"123"
) will translate to
{
"username": "mike",
"password" : "123"
}
but what the above one will translate to?
In the code you used for the example, $fields
is an object. Then, username
is a variable or property of the object. So, you end up with the array:
{
"username": $fields->username,
"password": $fields->password
}
If you are confused by the arrow syntax for objects, read the documentation on using objects for PHP.
$this
refers to the current instance of the class that this code is in.
profile
refers to the class member profile, which itself is an instance of some class.
create
is a function in that class, and is now being invoked.
The variable $fields
is being passed as an argument to the function.