Re: Exceptions for method on non-object rather than fatal (desired feature?)
On 21/02/12 17:06, Ralf Lang wrote:
> Am 21.02.2012 16:55, schrieb Martin Amps:
>> Could you not implement such functionality within your class as follows:
>>
>> class Family {
>> public function getMother() {
>> if ($this->hasMother())
>> return $someObj;
>> else
>> throw new BadMethodCallException(...);
>> }
>> }
>>
>> try {
>> $granny_name = $baby->getMother()->getMother()->getName();
>> } catch (BadMethodCallException $ex) {
>>
>> }
>>
>> Martin
>
> Hi Martin,
>
> First of all, thank you for your rapid response but it doesn't address
> my question (or at least the question I intended to ask)
>
> of course there is always a way to script around issues rather than
> adressing them. I just want to know if the change would be desired. I
> have code around which does exactly this, check on all levels of all
> possible combinations. Semantically, this is nonsense and it bloats
> the code. It's just a requirement because currently PHP handles this
> as non-recoverable fatal.
>
> I see no reason why it would be not desirable to have PHP raise the
> exception rather than putting more or less repeating code snippets all
> around the place. That is why I am asking.
You must be returning false/null somewhere. It's the same effor to
instead throw an exception or to return a Ghost family member.
Thread (18 messages)