Reputation: 4428
I am trying to read some php code, but can't understand how the variable $predicate here can be used:
private function getRelatedItems() {
$predicate = Zotero_Relations::$relatedItemPredicate;
$relations = $this->getRelations();
if (empty($relations->$predicate)) {
return [];
}
$relatedItemURIs = is_string($relations->$predicate)
? [$relations->$predicate]
: $relations->$predicate;
// Pull out object values from related-item relations, turn into items, and pull out keys
$keys = [];
foreach ($relatedItemURIs as $relatedItemURI) {
$item = Zotero_URI::getURIItem($relatedItemURI);
if ($item) {
$keys[] = $item->key;
}
}
return $keys;
}
As far as I can see $predicate is assigned a value that is never used. But I guess I am misunderstanding the scope somehow, or?
Upvotes: 0
Views: 65
Reputation: 1132
The variable is used to access a property in $relations
variable.
Here is a simplified usage example of variable property access which should clarify the usage for you:
$property = 'firstName';
$data = (object) array(
'firstName' => 'Justin',
'lastName' => 'Case',
);
echo $data->$property; //Rquivalent to $data->firstName, which eEchoes 'Justin'
Additionally, it works for functions, and class methods, too:
class Foo
{
public function bar()
{
echo 'Hello, world!';
}
}
$foo = new Foo();
$method = 'bar';
$foo->$method(); //Equivalent to $foo->bar(); Displays 'Hello, world!'
$function = 'strlen';
echo $function('Hello, world!'); //Is equivalent to calling strlen('Hello, world!');
It's pretty neat!
Upvotes: 1
Reputation: 21246
The $predicate
variable holds the name of an attribute on the $relations
object. E.g:
$relations->$predicate
If $predicate
is set to foo
then PHP sees the line as:
$relations->foo
Upvotes: 3