Beware that the "valid()" method should not be used to check the availability of an object before calling "get()". Indeed, the garbage collector might be triggered between the call to "valid()" and the call to "get()".
<?php
if ($weakRef->valid()) { $obj = $weakRef->get(); $obj->doSomeStuff(); }
?>
So instead, you should always call "get()" directly:
<?php
$obj = $weakRef->get();
if ($obj !== null) {
$obj->doSomeStuff(); }
?>
Generally speaking, the "valid()" method is tricky because a call to it might return true and the next call might return false (you never know when the garbage collector will be trigerred). However, testing for invalidity works reliably.
<?php
if ($weakRef->valid()) {
}
if (!$weakRef->valid()) {
}
?>