Law of Demeter is about dependencies not -> (or dots in other languages).
For example if you have a fluent interface (where methods return this) this law does not apply.
You want your class to depend on minimal number of other classes.
If your class depends on 10 other classes, a change in any of them can break it. It's harder to understand what your class does. It's also hard to test such a class.
It's doesn't matter if you have $field->getDependency1()->getDependency2()
or $field->dependency1->dependency2
- your code stills depends two other classes and knows internal structure of dependency1 (that it has dependency2 inside).
You can solve your problem with tell don't ask principle.
In OOD you don't want to use structures that just hold data. You want to have objects that know what to do with their fields.
For example you can refactor:
$oldBalance = $bank->getAccount()->getBalance();
$bank->getAccount()->setBalance($oldBalance - $amount);
To:
$bank->withdraw($amount);
In bank class:
function withdraw($amount) {
$this->account->withdraw($amount);
}
In Account class:
function withdraw($amount) {
$this->balance = $this->balance - $amount;
}
You have more methods but now the code that uses $bank, knows nothing about Account and its balance. You can easily tests that class mocking only $bank field. You have also more reusable code.