Menu

#35 Weirdness with non-string blank node labels

open
nobody
Model (10)
5
2007-08-13
2007-08-13
No

Blank nodes break when created with non-string labels. This is because equality tests on blank nodes apparently use PHP's == operator, which sometimes behaves weirdly (e.g. 1 == "1a" is true).

Example that shows the problem:

define("RDFAPI_INCLUDE_DIR", "rdfapi-php/api/");
include(RDFAPI_INCLUDE_DIR . "RdfAPI.php");

$model = ModelFactory::getDefaultModel();
$property = new Resource("http://example.com/p");
$resource1 = new BlankNode(1);
$resource2 = new BlankNode("1a");
$model->add(new Statement($resource1, $property, $resource2));
$model->add(new Statement($resource2, $property, new Literal("Test")));
echo $model->writeRdfToString();

This creates an incorrect serialization. The output is equivalent to

_:1 :p _:1a .
_:1 :p "Test" .

But it should be

_:1 :p _:1a .
_:1a :p "Test" .

As a fix, I suggest to always cast blank node labels to string in the BlankNode() constructor.

The documentation for the BlankNode constructor could also be improved: The variable name $namespace_or_uri_or_model is confusing, it should be $label_or_model. And the second argument $localName doesn't make any sense AFAICT and should be deprecated.

Discussion